代码如下:(一个form1窗体和一个Combo控件),请问各位大侠如何修改能支持文字.
Option Explicit
Private Sub cb1_KeyPress(KeyAscii As Integer)
KeyAscii = AutoMatchCBBox(cb1, KeyAscii)
End SubPrivate Sub Form_Initialize()
Dim count As Integer, index As Integer, aDate As Date
Randomize
count = Int((25 - 5 + 1) * Rnd) + 5
aDate = Date
Do While count > 0
Randomize
cb1.AddItem Format(aDate + Int(365 * Rnd), "mmm dd, yyyy")
count = count - 1
Loop
cb1.ListIndex = 0
End Sub
模块(AutoCompleteComboBoxModule.bas)
Option Explicit
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 Const CB_ERR = -1, CB_SELECTSTRING = &H14D, CB_SHOWDROPDOWN = &H14F, CBN_SELENDOK = 9
Public Function AutoMatchCBBox(ByRef cbBox As ComboBox, ByVal KeyAscii As Integer) As Integer
Dim strFindThis As String, bContinueSearch As Boolean
Dim lResult As Long, lStart As Long, lLength As Long
AutoMatchCBBox = 0 ' block cbBox since we handle everything
bContinueSearch = True
lStart = cbBox.SelStart
lLength = cbBox.SelLength On Error GoTo ErrHandle
If KeyAscii < 32 Then 'control char
bContinueSearch = False
cbBox.SelLength = 0 'select nothing since we will delete/enter
If KeyAscii = Asc(vbBack) Then 'take care BackSpace and Delete first
If lLength = 0 Then 'delete last char
If Len(cbBox) > 0 Then ' in case user delete empty cbBox
cbBox.Text = Left(cbBox.Text, Len(cbBox) - 1)
End If
Else 'leave unselected char(s) and delete rest of text
cbBox.Text = Left(cbBox.Text, lStart)
End If
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
ElseIf KeyAscii = vbKeyReturn Then 'user select this string
cbBox.SelStart = Len(cbBox)
lResult = SendMessage(cbBox.hwnd, CBN_SELENDOK, 0, 0)
AutoMatchCBBox = KeyAscii 'let caller a chance to handle "Enter"
End If
Else 'generate searching string
If lLength = 0 Then
strFindThis = cbBox.Text & Chr(KeyAscii) 'No selection, append it
Else
strFindThis = Left(cbBox.Text, lStart) & Chr(KeyAscii)
End If
End If
If bContinueSearch Then 'need to search
Call VBComBoBoxDroppedDown(cbBox) 'open dropdown list
lResult = SendMessage(cbBox.hwnd, CB_SELECTSTRING, -1, ByVal strFindThis)
If lResult = CB_ERR Then 'not found
cbBox.Text = strFindThis 'set cbBox as whatever it is
cbBox.SelLength = 0 'no selected char(s) since not found
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
Else
'found string, highlight rest of string for user
cbBox.SelStart = Len(strFindThis)
cbBox.SelLength = Len(cbBox) - cbBox.SelStart
End If
End If
On Error GoTo 0
Exit Function
ErrHandle:
'got problem, simply return whatever pass in
Debug.Print "Failed: AutoCompleteComboBox due to : " & Err.Description
Debug.Assert False
AutoMatchCBBox = KeyAscii
On Error GoTo 0
End Function'open dorpdown list
Private Sub VBComBoBoxDroppedDown(ByRef cbBox As ComboBox)
Call SendMessage(cbBox.hwnd, CB_SHOWDROPDOWN, Abs(True), 0)
End Sub
Option Explicit
Private Sub cb1_KeyPress(KeyAscii As Integer)
KeyAscii = AutoMatchCBBox(cb1, KeyAscii)
End SubPrivate Sub Form_Initialize()
Dim count As Integer, index As Integer, aDate As Date
Randomize
count = Int((25 - 5 + 1) * Rnd) + 5
aDate = Date
Do While count > 0
Randomize
cb1.AddItem Format(aDate + Int(365 * Rnd), "mmm dd, yyyy")
count = count - 1
Loop
cb1.ListIndex = 0
End Sub
模块(AutoCompleteComboBoxModule.bas)
Option Explicit
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 Const CB_ERR = -1, CB_SELECTSTRING = &H14D, CB_SHOWDROPDOWN = &H14F, CBN_SELENDOK = 9
Public Function AutoMatchCBBox(ByRef cbBox As ComboBox, ByVal KeyAscii As Integer) As Integer
Dim strFindThis As String, bContinueSearch As Boolean
Dim lResult As Long, lStart As Long, lLength As Long
AutoMatchCBBox = 0 ' block cbBox since we handle everything
bContinueSearch = True
lStart = cbBox.SelStart
lLength = cbBox.SelLength On Error GoTo ErrHandle
If KeyAscii < 32 Then 'control char
bContinueSearch = False
cbBox.SelLength = 0 'select nothing since we will delete/enter
If KeyAscii = Asc(vbBack) Then 'take care BackSpace and Delete first
If lLength = 0 Then 'delete last char
If Len(cbBox) > 0 Then ' in case user delete empty cbBox
cbBox.Text = Left(cbBox.Text, Len(cbBox) - 1)
End If
Else 'leave unselected char(s) and delete rest of text
cbBox.Text = Left(cbBox.Text, lStart)
End If
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
ElseIf KeyAscii = vbKeyReturn Then 'user select this string
cbBox.SelStart = Len(cbBox)
lResult = SendMessage(cbBox.hwnd, CBN_SELENDOK, 0, 0)
AutoMatchCBBox = KeyAscii 'let caller a chance to handle "Enter"
End If
Else 'generate searching string
If lLength = 0 Then
strFindThis = cbBox.Text & Chr(KeyAscii) 'No selection, append it
Else
strFindThis = Left(cbBox.Text, lStart) & Chr(KeyAscii)
End If
End If
If bContinueSearch Then 'need to search
Call VBComBoBoxDroppedDown(cbBox) 'open dropdown list
lResult = SendMessage(cbBox.hwnd, CB_SELECTSTRING, -1, ByVal strFindThis)
If lResult = CB_ERR Then 'not found
cbBox.Text = strFindThis 'set cbBox as whatever it is
cbBox.SelLength = 0 'no selected char(s) since not found
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
Else
'found string, highlight rest of string for user
cbBox.SelStart = Len(strFindThis)
cbBox.SelLength = Len(cbBox) - cbBox.SelStart
End If
End If
On Error GoTo 0
Exit Function
ErrHandle:
'got problem, simply return whatever pass in
Debug.Print "Failed: AutoCompleteComboBox due to : " & Err.Description
Debug.Assert False
AutoMatchCBBox = KeyAscii
On Error GoTo 0
End Function'open dorpdown list
Private Sub VBComBoBoxDroppedDown(ByRef cbBox As ComboBox)
Call SendMessage(cbBox.hwnd, CB_SHOWDROPDOWN, Abs(True), 0)
End Sub
解决方案 »
- 如何将一个已通过OPEN打开的recorderset新加入一个字段呢?
- 绑定SQL数据后怎么调整MSHFlexGrid表格控件中数据的显示格式.
- 用VB有没有什么读写XML的好方法或API?
- 如何用VB6.0存取ORACLE7.3中的BLOB类型字段(图片)
- 老问题又来了,我是真的需要你的帮助!关于S端数据库更新后如何通知C端如何实时刷新!
- 关于VSFlexGrid1
- 小弟在做个应用程序的主界面.需要些图片.但是不知道VB中的HEIGHT,WIDTH单位和象素单位的换算求解
- 在vb中可以做动画吗?
- 我想在vb程序中备份、还原数据库,遇到一些问题,求解!!!!
- 如何在VB中直接嵌套SQL检索出一列的最大值?急急!!
- 烟龄3年以上的进来报道
- 谁知道用水晶报表打印出来的照片为什么是黑白的?
top
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const CB_ERR = -1, CB_SELECTSTRING = &H14D, CB_SHOWDROPDOWN = &H14F, CBN_SELENDOK = 9
Public Function AutoMatchCBBox(ByRef cbBox As ComboBox, ByVal KeyAscii As Integer) As Integer
Dim strFindThis As String, bContinueSearch As Boolean
Dim lResult As Long, lStart As Long, lLength As Long
AutoMatchCBBox = 0 ' block cbBox since we handle everything
bContinueSearch = True
lStart = cbBox.SelStart
lLength = cbBox.SelLength On Error GoTo ErrHandle
If Abs(KeyAscii) < 256 Then
If KeyAscii < 32 Then 'control char
bContinueSearch = False
cbBox.SelLength = 0 'select nothing since we will delete/enter
If KeyAscii = Asc(vbBack) Then 'take care BackSpace and Delete first
If lLength = 0 Then 'delete last char
If Len(cbBox) > 0 Then ' in case user delete empty cbBox
cbBox.Text = Left(cbBox.Text, Len(cbBox) - 1)
End If
Else 'leave unselected char(s) and delete rest of text
cbBox.Text = Left(cbBox.Text, lStart)
End If
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
ElseIf KeyAscii = vbKeyReturn Then 'user select this string
cbBox.SelStart = Len(cbBox)
lResult = SendMessage(cbBox.hwnd, CBN_SELENDOK, 0, 0)
AutoMatchCBBox = KeyAscii 'let caller a chance to handle "Enter"
End If
Else 'generate searching string
If lLength = 0 Then
strFindThis = cbBox.Text & Chr(KeyAscii) 'No selection, append it
Else
strFindThis = Left(cbBox.Text, lStart) & Chr(KeyAscii)
End If
End If
Else
If lLength = 0 Then
strFindThis = cbBox.Text & Chr(KeyAscii) 'No selection, append it
Else
strFindThis = Left(cbBox.Text, lStart) & Chr(KeyAscii)
End If
End If If bContinueSearch Then 'need to search
Call VBComBoBoxDroppedDown(cbBox) 'open dropdown list
lResult = SendMessage(cbBox.hwnd, CB_SELECTSTRING, -1, ByVal strFindThis)
If lResult = CB_ERR Then 'not found
cbBox.Text = strFindThis 'set cbBox as whatever it is
cbBox.SelLength = 0 'no selected char(s) since not found
cbBox.SelStart = Len(cbBox) 'set insertion position @ the end of string
Else
'found string, highlight rest of string for user
cbBox.SelStart = Len(strFindThis)
cbBox.SelLength = Len(cbBox) - cbBox.SelStart
End If
End If
On Error GoTo 0
Exit Function
ErrHandle:
'got problem, simply return whatever pass in
Debug.Print "Failed: AutoCompleteComboBox due to : " & Err.Description
Debug.Assert False
AutoMatchCBBox = KeyAscii
On Error GoTo 0
End Function'open dorpdown list
Private Sub VBComBoBoxDroppedDown(ByRef cbBox As ComboBox)
Call SendMessage(cbBox.hwnd, CB_SHOWDROPDOWN, Abs(True), 0)
End Sub