VB不用鼠标点击combo1控件就能触发显示内容怎么写
注意Combo1控件里我已经Additem很多内容。我是想输入其中一个内容在Combo1控件的Text上那么Combo1便自动显示这些内容。就类似一种搜索功能。请问要怎么...
注意Combo1控件里我已经Additem很多内容。我是想输入其中一个内容在Combo1控件的Text上那么Combo1便自动显示这些内容。就类似一种搜索功能。请问要怎么写呢?
展开
3个回答
展开全部
给个例子 你试试
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
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd _
As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_SHOWDROPDOWN = &H14F
Private Const WM_SETREDRAW As Long = &HB&
Private Const CB_FINDSTRING As Long = &H14C&
'iKeyCode目的:传递KeyDown事件中的KeyCode
Private iKeyCode As Integer
Public Sub SearchCombo(InControl As Object)
On Error GoTo errTrap
Dim StrPos As Long
Dim lPos As Long
Dim SearchStr As String
If TypeOf InControl Is ComboBox Then
StrPos = InControl.SelStart
SearchStr = Left$(InControl.Text, StrPos)
lPos = SendMessage(InControl.hwnd, CB_FINDSTRING, 0, ByVal SearchStr)
If lPos >= 0 Then
InControl.Text = InControl.List(lPos)
InControl.ListIndex = lPos
End If
With InControl
.SelStart = StrPos
.SelLength = Len(InControl.Text)
End With
End If
Exit Sub
errTrap:
MsgBox Err.Description
End Sub
Private Sub Combo1_Change()
If iKeyCode <> vbKeyBack And iKeyCode <> vbKeyReturn Then
SearchCombo Combo1
SendMessageLong Combo1.hwnd, CB_SHOWDROPDOWN, True, 0
End If
End Sub
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
iKeyCode = KeyCode
End Sub
Private Sub Form_Load()
Combo1.AddItem "中国"
Combo1.AddItem "中非"
Combo1.AddItem "美国"
Combo1.AddItem "意大利"
Combo1.AddItem "菲律宾"
Combo1.AddItem "俄罗斯"
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
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd _
As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_SHOWDROPDOWN = &H14F
Private Const WM_SETREDRAW As Long = &HB&
Private Const CB_FINDSTRING As Long = &H14C&
'iKeyCode目的:传递KeyDown事件中的KeyCode
Private iKeyCode As Integer
Public Sub SearchCombo(InControl As Object)
On Error GoTo errTrap
Dim StrPos As Long
Dim lPos As Long
Dim SearchStr As String
If TypeOf InControl Is ComboBox Then
StrPos = InControl.SelStart
SearchStr = Left$(InControl.Text, StrPos)
lPos = SendMessage(InControl.hwnd, CB_FINDSTRING, 0, ByVal SearchStr)
If lPos >= 0 Then
InControl.Text = InControl.List(lPos)
InControl.ListIndex = lPos
End If
With InControl
.SelStart = StrPos
.SelLength = Len(InControl.Text)
End With
End If
Exit Sub
errTrap:
MsgBox Err.Description
End Sub
Private Sub Combo1_Change()
If iKeyCode <> vbKeyBack And iKeyCode <> vbKeyReturn Then
SearchCombo Combo1
SendMessageLong Combo1.hwnd, CB_SHOWDROPDOWN, True, 0
End If
End Sub
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
iKeyCode = KeyCode
End Sub
Private Sub Form_Load()
Combo1.AddItem "中国"
Combo1.AddItem "中非"
Combo1.AddItem "美国"
Combo1.AddItem "意大利"
Combo1.AddItem "菲律宾"
Combo1.AddItem "俄罗斯"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询