Home > Archive > Visual Basic > August 2005 > Type-ahead on a combo box
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Type-ahead on a combo box
|
|
| Dave Rudolf 2005-08-27, 6:55 pm |
| Hey all,
I have a little VB6 app that I'm working on, and I want to change the
behavior of my combo boxes. At present, if I put the control focus on one,
then press, say, the 'S' key, its list goes to the first item that starts
with the letter S. If I then press the letter 'A', it goes to the first item
that starts with 'A'. I would rather that it went to the first item that
starts with 'SA'. I seem to remember that there was some way to configure a
combo box to do this, but I can't find a way. Any ideas?
Thanks.
Dave
| |
|
| Option Explicit
Dim strCombo As String
Const WM_SETREDRAW = &HB
Const KEY_A = 65
Const KEY_Z = 90
Dim x%
Dim strTemp$
Dim nRet&
Sub AutoFillCombo(KeyCode As Integer, Shift As Integer, cbX As ComboBox)
If KeyCode >= KEY_A And KeyCode <= KEY_Z Then
'only look at letters A-Z
strTemp = cbX.Text
If Len(strTemp) = 1 Then strCombo = strTemp
nRet& = SendMessage(cbX.hWnd, _
WM_SETREDRAW, False, 0&)
For x = 0 To (cbX.ListCount - 1)
If UCase((strTemp & Mid$(cbX.List(x), Len(strTemp) + 1))) =
UCase(cbX.List(x)) Then
cbX.ListIndex = x
cbX.Text = cbX.List(x)
cbX.SelStart = Len(strTemp)
cbX.SelLength = _
Len(cbX.Text) - (Len(strTemp))
strCombo = strCombo & _
Mid$(strTemp, Len(strCombo) + 1)
Exit For
Else
If InStr(UCase(strTemp), UCase(strCombo)) Then
strCombo = strCombo & Mid$(strTemp, Len(strCombo) + 1)
cbX.Text = strCombo
cbX.SelStart = Len(cbX.Text)
Else
strCombo = strTemp
End If
End If
Next
nRet& = SendMessage(cbX.hWnd, _
WM_SETREDRAW, True, 0&)
End If
Exit Sub
End Sub
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Call AutoFillCombo(KeyCode, Shift, Combo1)
End Sub
Limitation is it will only autofill if combo item starts with alphabeth...
You can place the autofillcombo code in module and call it from any combo
keyup event.
"Dave Rudolf" <dave.rudolf@usask.ca> wrote in message
news:uazE3wxqFHA.1256@TK2MSFTNGP09.phx.gbl...
> Hey all,
>
> I have a little VB6 app that I'm working on, and I want to change the
> behavior of my combo boxes. At present, if I put the control focus on one,
> then press, say, the 'S' key, its list goes to the first item that starts
> with the letter S. If I then press the letter 'A', it goes to the first
item
> that starts with 'A'. I would rather that it went to the first item that
> starts with 'SA'. I seem to remember that there was some way to configure
a
> combo box to do this, but I can't find a way. Any ideas?
>
> Thanks.
>
> Dave
>
>
| |
| Rick Rothstein [MVP - Visual Basic] 2005-08-27, 6:55 pm |
| > I have a little VB6 app that I'm working on, and I want to change the
> behavior of my combo boxes. At present, if I put the control focus on
one,
> then press, say, the 'S' key, its list goes to the first item that
starts
> with the letter S. If I then press the letter 'A', it goes to the
first item
> that starts with 'A'. I would rather that it went to the first item
that
> starts with 'SA'. I seem to remember that there was some way to
configure a
> combo box to do this, but I can't find a way. Any ideas?
http://vbnet.mvps.org/code/listapi/findinglistitems.htm
Rick
| |
| Jeff Johnson [MVP: VB] 2005-08-29, 3:55 am |
|
"Dave Rudolf" <dave.rudolf@usask.ca> wrote in message
news:uazE3wxqFHA.1256@TK2MSFTNGP09.phx.gbl...
> I have a little VB6 app that I'm working on, and I want to change the
> behavior of my combo boxes. At present, if I put the control focus on one,
> then press, say, the 'S' key, its list goes to the first item that starts
> with the letter S. If I then press the letter 'A', it goes to the first
> item that starts with 'A'. I would rather that it went to the first item
> that starts with 'SA'. I seem to remember that there was some way to
> configure a combo box to do this, but I can't find a way. Any ideas?
If the Sorted property of the combo box is True then Windows will handle
this for you. If for some reason you can't have Sorted set to True then
you're stuck with emulating this behavior in code.
| |
| Dave Rudolf 2005-08-29, 6:55 pm |
| "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:ewpxCgFrFHA.2624@TK2MSFTNGP15.phx.gbl...
>
>
> If the Sorted property of the combo box is True then Windows will handle
> this for you. If for some reason you can't have Sorted set to True then
> you're stuck with emulating this behavior in code.
>
Thanks. That works almost perfectly. The only problem I now have is that it
causes a "Click" event every time a button is pressed. I would much rather
have certain code run only when the combo box is actually clicked. How might I
do that?
Dave
| |
| Jeff Johnson [MVP: VB] 2005-08-29, 6:55 pm |
|
"Dave Rudolf" <dave.rudolf@usask.ca> wrote in message
news:ejrEIYLrFHA.2624@TK2MSFTNGP15.phx.gbl...
>
> Thanks. That works almost perfectly. The only problem I now have is that
> it
> causes a "Click" event every time a button is pressed. I would much rather
> have certain code run only when the combo box is actually clicked. How
> might I
> do that?
You cannot stop the Click event from occurring. In order to distinguish
mouse clicks from typing you'd more than likely have to subclass the combo
box and examine all input to it. A hack would be to disable/enable a timer
in the Click event and run the normal selection code only in the timer
event. That way you'd reset the timer every time the user presses a key and
wouldn't run the selection code until the user has stopped typing for a
certain amount of time (which you can tweak by trial and error).
| |
| Jan Hyde 2005-08-30, 3:55 am |
| "Dave Rudolf" <dave.rudolf@usask.ca>'s wild thoughts were
released on Mon, 29 Aug 2005 10:28:56 -0600 bearing the
following fruit:
>"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
>news:ewpxCgFrFHA.2624@TK2MSFTNGP15.phx.gbl...
>
>Thanks. That works almost perfectly. The only problem I now have is that it
>causes a "Click" event every time a button is pressed. I would much rather
>have certain code run only when the combo box is actually clicked. How might I
>do that?
>
How would the user select an item with the keyboard?
If you disable this click event
Typing SA
Select the first item starting with SA, lets say that the
item the user wants.
Since the click event did not fire the user would then have
to use the mouse to select a different item, then select the
item again with the mouse....
Jan Hyde (VB MVP)
--
A woman was in love with fourteen soldiers - it was clearly platoonic
[Abolish the TV Licence - http://www.tvlicensing.biz/]
| |
| Karl E. Peterson 2005-08-30, 6:55 pm |
| Dave Rudolf wrote:
> "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
> news:ewpxCgFrFHA.2624@TK2MSFTNGP15.phx.gbl...
>
> Thanks. That works almost perfectly. The only problem I now have is
> that it causes a "Click" event every time a button is pressed. I
> would much rather have certain code run only when the combo box is
> actually clicked. How might I do that?
See if this does what you want... http://vb.mvps.org/samples/ListSearch
--
Working Without a .NET?
http://classicvb.org/petition
|
|
|
|
|