意思是:假如我的ComboBox的list中有abcd, abde, acdf等等项目。如果我开始输入a,combobox应当立即显示abcd,并且,bcd是选中状态,以便用户继续输入。如果第二个输入c,则马上显示acdf,并且df是选中状态。我说的combobox是dropdown样式的。
解决方案 »
- VB.NET中自定义创建控件的问题
- 困繞的 ActiveX 版本錯誤!
- 求 visual basic 开发经验技术宝典 下载地址
- 关于下载问题
- 查询多个表的Recordset的添加问题
- 如何用vb去读取excel中文本域表单的内容
- 求用“最小二乘法”滤波的例子!急!在线等待!
- 求助!!!如何使vb程序,最小化后类似qq一样,只有图标在任务栏的通知区域.
- 求教:用什么函数single 算得的结果只保留两位小数?
- 小弟的打印机控制码问题,有谁放马过来,把它高定??????
- how to make the width of a statusbar = form width and automatic resize?
- 如何显示平滑的字体?
Public Const LB_FINDSTRING& = &H18F
Public Const CB_FINDSTRINGEXACT& = &H14C
Public Const EM_GETSEL = &HB0
Public Const EM_LINEFROMCHAR = &HC9
Public Const EM_LINEINDEX = &HBB
Public Const CB_GETEDITSEL = &H140
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long------------------------------------form1
Dim ol As Integer
Dim isKeyPressed As Boolean
Dim isKeyBackPressed As Boolean
Dim isAnySelectedChar As Boolean
Dim BackLen As Integer
Dim aStr As String
Dim I As Integer
Dim beforeStr As String
Dim afterStr As String
Dim cursorPos As Long
Dim selLen As Integer'²Ù×ÝComboxµÄÐÐΪ
Private Sub Combo1_Change()
Dim Blen As Integer
Dim pos As Long afterStr = Combo1.Text'Èç¹û·Ç¹¦Äܼü£¨Enter¡¢BackSpace£©°´ÏÂ
If isKeyPressed Then
ol = Len(Combo1.Text)
DoEvents
I = SendMessage(Combo1.hwnd, CB_FINDSTRINGEXACT, -1, ByVal CStr(Combo1.Text))
If I >= 0 Then
Combo1.ListIndex = I
Combo1.SelStart = ol
Combo1.SelLength = Len(Combo1.Text) - ol
Combo1.Refresh
Else
pos = cursorPos + (Len(afterStr) - (Len(beforeStr) - selLen))
'pos = FindInsertPos(beforeStr, afterStr)
Combo1.SelStart = pos
Combo1.SelLength = 0
End If
'MsgBox beforeStr & "-" & afterStr
'·ÀÖ¹OnChangeʼþ×Ô¼¤£¨ÓÉÓڸıäÁËComboxµÄÄÚÈÝ£©
isKeyPressed = False
End If'Èç¹ûBackSpace¼ü°´Ï£¬²Á³ýβ²¿×Ö·û
If isKeyBackPressed Then 'aStr = Combo1.Text
'Èç¹û°´ÏÂBackSpace¼ü֮ǰÓÐ×Ö·ûÑ¡ÖУ¬ÇÒÑ¡ÖеÄ×Ö·ûÔÚβ²¿£¬Í¬Ê±×Ö·û´®³¤¶È²»Ð¡ÓÚ1
If isAnySelectedChar Then
If Len(afterStr) >= 1 Then
afterStr = Left$(afterStr, Len(afterStr) - 1)
End If
End If ol = Len(afterStr)
DoEvents
'ÕÒ³öComboxÖÐ×î·ûºÏÊäÈë×Ö´®µÄÏîµÄϱê
I = SendMessage(Combo1.hwnd, CB_FINDSTRINGEXACT, -1, ByVal CStr(afterStr))
'Èç¹ûûÕÒµ½
If I < 0 Then
pos = cursorPos + (Len(afterStr) - Len(beforeStr))
'Èç¹ûɾ³ýµÄ×Ö·û²»ÔÚβ²¿£¬ÉèÖùâ±êÔÚɾ³ýλÖÃ
If (pos < Len(Combo1.Text)) And (pos > 0) Then
Combo1.SelStart = pos
Combo1.SelLength = 0
Else
Combo1.ListIndex = I
Combo1.SelStart = ol
Combo1.SelLength = Len(Combo1.Text) - ol
'Èç¹ûÊäÈëµÄ×Ö´®ÒѾ­Îª¿Õ£¬ÉèÖÃComboxÏÔʾ¿Õ
If Len(afterStr) = 0 Then
Combo1.Text = ""
End If
Combo1.Refresh
End If
'Èç¹ûÕÒµ½£¬Ë¢ÐÂComboxÏÔʾ£¬²¢Ñ¡ÖзÇÊäÈë×Ö´®
Else
I = SendMessage(Combo1.hwnd, CB_FINDSTRINGEXACT, -1, ByVal CStr(afterStr))
Combo1.ListIndex = I
'Èç¹ûÔÚ°´BackSpace¼ü֮ǰÓÐ×Ö·ûÑ¡ÖУ¬ÔòBackSpaceÖ»²Á³ýÁËÑ¡ÖеÄ×Ö·û¡£Òò´Ë£¬Ç¿ÖƲÁȥβ²¿×îºóÒ»¸ö×Ö·û
'If isAnySelectedChar Then
' Combo1.SelStart = ol - 1
'Else
Combo1.SelStart = ol
'End If
Combo1.SelLength = Len(Combo1.Text) - ol + 1
Combo1.Refresh
End If
'·ÀÖ¹×Ô¼¤
isKeyPressed = False
isKeyBackPressed = False
End IfEnd Sub
'²¶»ñÔÚComboxÉϵļüÅ̶¯×÷
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim obj As Object
Dim I As Long
Dim j As Long
isKeyPressed = True
cursorPos = GetCaretPos(Me.ActiveControl.hwnd, I, j)
'Èç¹ûEnter¼ü°´Ï£¬Ê§È¥½¹µã
If KeyAscii = vbKeyReturn Then
Set obj = FindNextControl(Me.ActiveControl.TabIndex)
If Not (obj Is Nothing) Then obj.SetFocus
Exit Sub
End If
'Èç¹ûBackSpace£¨»ØÍ˼ü£©°´Ï£¬²Á³ý×îºóÒ»¸ö×Ö
If KeyAscii = vbKeyBack Then
isKeyPressed = False
isKeyBackPressed = Not isKeyPressed
End If
'ÉèÖá°ÔÚBackSpace¼ü°´ÏÂ֮ǰ£¬ÊÇ·ñÓÐÑ¡ÖеÄ×Ö·û¡±£¬Í¬Ê±¼ì²éÊÇ·ñÑ¡ÖеÄÊÇÖмäµÄ×Ö·û
If Combo1.SelLength > 0 Then
isAnySelectedChar = True
selLen = Combo1.SelLength
cursorPos = cursorPos - selLen
Else
isAnySelectedChar = False
selLen = 0
End If
beforeStr = Combo1.Text
End Sub
'ÕÒ³öÏÂÒ»¸ö¿ÉÒÔ»ñµÃ½¹µãµÄ¿Ø¼þ
Private Function FindNextControl(ByVal tIndex As Integer) As Control
Dim obj As Control
Dim tempIndex As Integer
Dim tempControl As Control
For Each obj In Me.Controls
If obj.TabIndex > tIndex Then
Set tempControl = obj
Exit For
End If
If obj.TabIndex < tempIndex Then
tempIndex = obj.TabIndex
Set tempControl = obj
End If
Next
Set FindNextControl = tempControl
End FunctionPrivate Function FindInsertPos(ByVal bStr As String, ByVal aStr As String) As Integer
Dim m As Integer
Dim n As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
m = Len(aStr)
n = Len(bStr)
k = m
l = 0 For j = 1 To IIf(m > n, n, m)
If Left$(aStr, j) <> Left$(bStr, j) Then
k = j
Exit For
End If
Next
If Not isKeyBackPressed Then
For j = k + 1 To m
If Left$(aStr, j) = Left$(bStr, k + 1) Then
l = j - 1
Exit For
End If
Next
If l <> 0 Then k = l
End If
FindInsertPos = k
End Function
Private Sub Form_Load()
Combo1.Clear
Combo1.AddItem "Apples"
Combo1.AddItem "Appies"
Combo1.AddItem "Banana"
Combo1.AddItem "Bread"
Combo1.AddItem "Break"
Combo1.AddItem "Bxuu"
Combo1.AddItem "Bxv1"
Combo1.AddItem "Bybb"
Combo1.AddItem "Bybb"
Combo1.AddItem "»ÆʯÈË"
Combo1.AddItem "»Æ¹âÏÀ"
Combo1.AddItem "ÀîÈôʯ"
End SubPublic Function GetCaretPos(ByVal hwnd5 As Long, sPos As Long, ePos As Long) As Long
Dim I As Long
I = SendMessage(hwnd5, CB_GETEDITSEL, sPos, ePos)
GetCaretPos = ePos
End Function
有些测试代码,请参考