ComboBox keyboard recognition?

  • Thread starter Thread starter Strahimir Antoljak
  • Start date Start date
S

Strahimir Antoljak

Could anyone help me with the following:

Is there a quick, or at least known way to
force a ComboBox, having DropDownStyle=
DropDownList, to recognize keyboard input
and perform Item selection based on that.

To be more specific: I have a list of 150 items
(all 7-digit numeric values) represented in
the combobox, and they are sorted. Instead
of scrolling to the specific item, I'd like to
be able to start typing on the keyboard and
have combobox recognize what I am typing,
and based on that, the selected value gets
changed. As I am progressing with the keyboard
input, the selection gets narrowed down.
Say I have to find number 1455601, and start
typing '1' on the keyboard, and first Item that starts
with '1' gets selected, then I type in '4', the second
digit, and then first item starting with '14' in the
combobox list gets selected, and I type in '5'
and selected item is first value starting with '145'
and so forth.

This behavior is very similar to Windows Explorer
(at least in WinXP) when there is a large number
of folders, and instead of scrolling up and down
the browser, typing in first few letters/numbers
of the folder, the sought folder gets selected.

Thanks in advance,

Strah
 
Hello Strah
You can use something like it but it will only find one
character...

Private Sub ComboBox1_KeyDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs) Handles
ComboBox1.KeyDown
Me.ComboBox1.FindString(e.KeyValue.ToString)
End Sub


Kind Regards
Jorge
 
Well, the OP can already do that because when the Style is set to dropdown
list, the first character is searchable, but subsequent characters are not.

Regards - OHM
 
Jorge,

thanks, but this is actually default behavior
of the ComboBox, so no code required.

I was going to use KeyDown event to
achieve what I need, but before starting
I wanted to double check if there was
something like that already available, just
to avoid re-inventing a wheel.

However, I was not aware of
ComboBox's FindString Method
which I will be using to build a
string of KeyValues provided by
the user.

Thanks again and Happy New Year,

Strah
 
This solution will 'remember' your keys
typed sequence for 1200 milliseconds
(1.2 secs) and then it will start over (that
is similar to WinExplorer).

If you want the procedure to remember it
for some other timespan, change:
Now.AddMilliseconds(1200)
to
Now.AddMilliseconds(iOtherValue)

Thanks,

Strah

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

Static sFindString As String = ""
Static HoldForFewSecs As DateTime = "1/1/0001 12:00:00 AM"

If HoldForFewSecs = "1/1/0001 12:00:00 AM" Then HoldForFewSecs =
Now.AddMilliseconds(1200)

Dim index As Integer
If Now < HoldForFewSecs Then
sFindString &= e.KeyChar.ToString
index = Me.ComboBox1.FindString(sFindString)
ComboBox1.SelectedIndex = index
Else
sFindString = e.KeyChar.ToString
index = Me.ComboBox1.FindString(sFindString)
ComboBox1.SelectedIndex = index
HoldForFewSecs = "1/1/0001 12:00:00 AM"
End If

End Sub
 
Thanx.

OHM

Strahimir said:
This solution will 'remember' your keys
typed sequence for 1200 milliseconds
(1.2 secs) and then it will start over (that
is similar to WinExplorer).

If you want the procedure to remember it
for some other timespan, change:
Now.AddMilliseconds(1200)
to
Now.AddMilliseconds(iOtherValue)

Thanks,

Strah

Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress

Static sFindString As String = ""
Static HoldForFewSecs As DateTime = "1/1/0001 12:00:00 AM"

If HoldForFewSecs = "1/1/0001 12:00:00 AM" Then
HoldForFewSecs = Now.AddMilliseconds(1200)

Dim index As Integer
If Now < HoldForFewSecs Then
sFindString &= e.KeyChar.ToString
index = Me.ComboBox1.FindString(sFindString)
ComboBox1.SelectedIndex = index
Else
sFindString = e.KeyChar.ToString
index = Me.ComboBox1.FindString(sFindString)
ComboBox1.SelectedIndex = index
HoldForFewSecs = "1/1/0001 12:00:00 AM"
End If

End Sub
 
Back
Top