vb combobox控件

想要实现在combobox输入汉字,下拉框自动弹出符合条件的内容。比如输入“陈”,即可弹出“陈好”等与陈匹配的名字。... 想要实现在combobox输入汉字,下拉框自动弹出符合条件的内容。
比如输入“陈”,即可弹出“陈好”等与陈匹配的名字。
展开
 我来答
百度网友e9042513a
2009-07-13 · TA获得超过6985个赞
知道大有可为答主
回答量:2388
采纳率:0%
帮助的人:3518万
展开全部
VB combo 随输入内容 调整下拉框

Option Explicit

Dim namestr()

Private Sub Form_Load()
ReDim namestr(1 To 5)
namestr(1) = "陈好"
namestr(2) = "陈很好"
namestr(3) = "陈特好"
namestr(4) = "陈贼好"
namestr(5) = "陈好不好"

End Sub

Private Sub Combo1_Change()
Static a As Boolean
If a Then Exit Sub

Dim i As Integer
Dim p As Integer
Dim s As String

a = True
If Combo1.Text = "" Then Exit Sub
p = Combo1.SelStart
s = Combo1.Text

Combo1.Clear
For i = 1 To 5
If InStr(1, namestr(i), s) = 1 Then Combo1.AddItem namestr(i)
Next i
Combo1.Text = s
Combo1.SelStart = p
a = False

End Sub
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wys_sans
2009-07-13 · TA获得超过4008个赞
知道大有可为答主
回答量:2474
采纳率:0%
帮助的人:1082万
展开全部
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_SETREDRAW = &HB
Const KEY_A = 65
Const KEY_Z = 90
Const CB_SHOWDROPDOWN = &H14F
Dim strCombo As String

Private Sub Combo1_Change()
Dim nret As Long
nret = SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0&)
End Sub

Private Sub combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim x%
Dim strTemp$
Dim nret&
If KeyCode >= KEY_A And KeyCode <= KEY_Z Then
'only look at letters A-Z
strTemp = Combo1.Text
If Len(strTemp) = 1 Then strCombo = strTemp
nret& = SendMessage(Combo1.hwnd, WM_SETREDRAW, False, 0&)
For x = 0 To (Combo1.ListCount - 1)
If UCase((strTemp & Mid$(Combo1.List(x), Len(strTemp) + 1))) = UCase(Combo1.List(x)) Then
Combo1.ListIndex = x
Combo1.Text = Combo1.List(x)
Combo1.SelStart = Len(strTemp)
Combo1.SelLength = Len(Combo1.Text) - (Len(strTemp))
strCombo = strCombo & Mid$(strTemp, Len(strCombo) + 1)
Exit For
Else
If InStr(UCase(strTemp), UCase(strCombo)) Then
strCombo = strCombo & Mid$(strTemp, Len(strCombo) + 1)
Combo1.Text = strCombo
Combo1.SelStart = Len(Combo1.Text)
Else
strCombo = strTemp
End If
End If
Next
nret& = SendMessage(Combo1.hwnd, WM_SETREDRAW, True, 0&)
End If
End Sub

Private Sub Form_Load()
Combo1.AddItem "abc"
Combo1.AddItem "bcd"
Combo1.AddItem "cde"
Combo1.AddItem "def"
Combo1.AddItem "efg"
Combo1.AddItem "fgh"
Combo1.AddItem "ghi"
Combo1.AddItem "hij"
End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式