|
-
Nov 28th, 2001, 07:24 AM
#1
Thread Starter
Frenzied Member
I found this function somewhere a while ago;
Stick this in a module;
Code:
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 CB_FINDSTRING = &H14C
Const CB_ERR = (-1)
Public Function AutoFind(ByRef cboCurrent As ComboBox, ByVal KeyAscii As Integer, Optional ByVal LimitToList As Boolean = False)
Dim lCB As Long
Dim sFindString As String
On Error GoTo Err_Handler
If KeyAscii = 8 Then
If cboCurrent.SelStart <= 1 Then
cboCurrent = ""
AutoFind = 0
Exit Function
End If
If cboCurrent.SelLength = 0 Then
sFindString = UCase(Left(cboCurrent, Len(cboCurrent) - 1))
Else
sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart - 1)
End If
ElseIf KeyAscii < 32 Or KeyAscii > 127 Then
Exit Function
Else
If cboCurrent.SelLength = 0 Then
sFindString = UCase(cboCurrent.Text & Chr$(KeyAscii))
Else
sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart) & Chr$(KeyAscii)
End If
End If
lCB = SendMessage(cboCurrent.hwnd, CB_FINDSTRING, -1, ByVal sFindString)
If lCB <> CB_ERR Then
cboCurrent.ListIndex = lCB
cboCurrent.SelStart = Len(sFindString)
cboCurrent.SelLength = Len(cboCurrent.Text) - cboCurrent.SelStart
AutoFind = 0
Else
If LimitToList = True Then
AutoFind = 0
Else
AutoFind = KeyAscii
End If
End If
Err_Handler:
End Function
Then, in the KeyPress event of the combobox in question - do this
Code:
Private Sub cboText_KeyPress(KeyAscii As Integer)
KeyAscii = AutoFind(cboText, KeyAscii, False)
End Sub
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Nov 28th, 2001, 07:26 AM
#2
Thread Starter
Frenzied Member
Oh, and if you want to limit the user to the items in the combobox you use
Code:
Private Sub cboText_KeyPress(KeyAscii As Integer)
KeyAscii = AutoFind(cboText, KeyAscii, True)
End Sub
instead
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Nov 28th, 2001, 10:18 AM
#3
Hyperactive Member
combo box
Thats exactly what I was looking for,
cheers
By any chance do you know if there is a way to open the combo box automatically when you start typing text in.
i.e. instead of pressing the down arrow to open it.
At present , when you open the combo by pressing the down "arrow" , it opens fine , but when you select an item the combo closes again.
any ideas...??
-
Nov 28th, 2001, 10:33 AM
#4
Fanatic Member
Code:
Private Const CB_SHOWDROPDOWN = &H14F
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_GETDROPPEDSTATE = &H157
Private Sub cmd1_Click()
Dim lRet&
lRet = SendMessage(cbo1.hwnd, CB_SHOWDROPDOWN, Not SendMessage(cbo1.hwnd, CB_GETDROPPEDSTATE, 0, 0), 0)
End Sub
Crispin
VB6 ENT SP5
VB.NET
W2K ADV SVR SP3
WWW.BLOCKSOFT.CO.UK
[Microsoft Basic: 1976-2001, RIP]
-
Nov 28th, 2001, 10:48 AM
#5
Hyperactive Member
thats works well , but when you press enter after your selected text appears , which a lot of people would instinctively do , the combo opens up , when you really want it to go down..
its not a major issue ....
-
Nov 29th, 2001, 02:10 PM
#6
Originally posted by DaveR
thats works well , but when you press enter after your selected
text appears , which a lot of people would instinctively do , the
combo opens up , when you really want it to go down..
Thats easily addressed DaveR.
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'user hit enter key
Dim lRet&
lRet = SendMessage(Combo1.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0&)
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|