is there a way when you press like a in a text box you can go to the a's in the combo box ex
user :
Hel
combobox: hello
is there a way to do this in vb
Printable View
is there a way when you press like a in a text box you can go to the a's in the combo box ex
user :
Hel
combobox: hello
is there a way to do this in vb
I remember denniswrenn answered this a while ago, I'll show him this post for ya ;)
thank you
It seems to be something simple. I'm throwing together a sample and I'll post it when I'm done. :)
how do i use the ShellExecute to open a html page thanks
Here's a sample that does what you asked about. I could have done a binary search function, but then I realized it would be simpler to build a string from the first letter of the combobox contents and use InStr instead. Paste this into a project with a combobox (Combo1) and a textbox (Text1). Remember to set the Sorted property of the combobox to True in the Properties window before you run this, or it won't work correctly.
Code:Option Explicit
Private Sub Form_Load()
'some test things
Combo1.AddItem "apple"
Combo1.AddItem "aardvark"
Combo1.AddItem "banana"
Combo1.AddItem "ball"
Combo1.AddItem "cherry"
Combo1.AddItem "doorknob"
Combo1.AddItem "eggplant"
Combo1.AddItem "easy"
Combo1.AddItem "test"
Combo1.AddItem "fruit"
End Sub
Private Function Find(Target As ComboBox, SearchKey As String) As Integer
Dim SearchDomain As String
Dim Position As Integer
Dim i As Integer
SearchDomain = ""
SearchKey = LCase$(SearchKey) 'remove case-sensitivity
Find = -1 'default is not finding anything
'if there's nothing in the combobox, leave function
If Target.ListCount = 0 Then Exit Function
'cycle through all items in the combobox, building a
'string using the first char of each item
For i = 0 To (Target.ListCount - 1)
SearchDomain = SearchDomain & LCase$(Left$(Target.List(i), 1))
Next i
'look for the first position of the desired key in the string
Position = InStr(SearchDomain, SearchKey)
'if it was found, return the position in the combobox corresponding
'to the found position (items in a combobox go from 0 to ComboBox.ListCount-1
If Position > 0 Then Find = Position - 1
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim NewIndex As Integer
'try to find the pressed key in the combobox
NewIndex = Find(Combo1, Chr$(KeyAscii))
'if found, change the combobox's currently selected index and text to show it
If NewIndex <> -1 Then
Combo1.ListIndex = NewIndex
Combo1.Text = Combo1.List(Combo1.ListIndex)
End If
End Sub
Aw man! InStr! I tried making a sample for ya on my own (failed) and I tried Left (what was I thinking!) apparently dennis hasn't been on yet...oh well
Yeah, I almost smacked myself when I realized I could make VB do the hard work. :D I was trying to make a binary search function, but had no success. I couldn't recall how to make the search thing drop into the "lower" half of the set, and of course that's 50% of the whole algorithm heh heh.