vb combobox控件
想要实现在combobox输入汉字,下拉框自动弹出符合条件的内容。比如输入“陈”,即可弹出“陈好”等与陈匹配的名字。...
想要实现在combobox输入汉字,下拉框自动弹出符合条件的内容。
比如输入“陈”,即可弹出“陈好”等与陈匹配的名字。 展开
比如输入“陈”,即可弹出“陈好”等与陈匹配的名字。 展开
2个回答
展开全部
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
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
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询