set next number of combo box list according to the number pressed

08-05-2005, 08:10 PM
im trying to make a smart combo box that will loop through itself when KeyPress is activated and set the number that is pressed.

i have a combo box with years loaded in it ranging from 1930 to 2005. when the user will press 1 it will automaticly go to the first number starting with one wich is 1999, then if the user will press 8 it will go to 1899 and so on.

just for the combo box style is set to "2 - Dropdown List"

i have tried doing this myself but i dont see where im going with this :confused:, so maybe you can help me.

this is where im stuck

Private Sub cboYr_KeyPress(KeyAscii As Integer)
Dim i As Integer
Dim srch As Byte
If KeyAscii >= 48 And KeyAscii <= 57 Then
For i = 0 To cboYr.ListCount - 1
If KeyAscii = cboYr.List(i) Then cboYr.Text = cboYr.List(i)
Next i
For srch = 1 To 5
For i = 0 To cboYr.ListCount - 1
If cboYr.List(i) = KeyAscii Then
cboYr.SelStart (srch - 1)
End If
Next i
Next i
End If
End Sub

thanks for your time!

08-05-2005, 08:42 PM
You need a static variable to do this, because you need a persistent way of keeping track of the digit for which you want to use whatever number the user enters. I used the keyUp event rather than keyPress to easily intercept ESC which I thought would be a useful way to restart the process:

Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)

Static strYear As String

If KeyCode = vbKeyEscape Then
strYear = ""
ElseIf (KeyCode < vbKey0) Or (KeyCode > vbKey9) Then
'numbers only
Exit Sub
'reset when year typed out completely
If Len(strYear) = 4 Then strYear = ""
'append number to year
strYear = strYear & (KeyCode - vbKey0)
'fill up with 9s
Dim strYearCeiling As String: strYearCeiling = strYear
Do While Len(strYearCeiling) < 4
strYearCeiling = strYearCeiling & "9"
If Val(strYearCeiling) < firstYear Then strYearCeiling = CStr(firstYear)
If Val(strYearCeiling) > lastyear Then strYearCeiling = CStr(lastyear)
'lookup corresponding entry
Combo1.ListIndex = Val(strYearCeiling) - firstYear
End If

'block generic lookup
KeyCode = 0

End Sub

firstYear and lastYear are global constants that define the used range, i.e. 1930-2005 in your example.
It might be good to set keyCode and keyAscii to zero as well in the keyDown and keyPress events, because as it is it briefly jumps to the first entry rather than 1999 when you start by pressing a "1", etc.

08-05-2005, 08:51 PM
This is probably easiest using Win32 API:

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 Sub Combo1_Change()
Combo1.ListIndex = SendMessage(Combo1.hwnd, CB_FINDSTRING, 0&, ByVal Combo1.Text)
End Sub

08-05-2005, 09:10 PM
OnErr0r - I can't get this to work for what cool_dude wants - i.e. entering "1", "9", "5" to bring up 1959...

08-05-2005, 09:14 PM
OnErr0r - I can't get this to work for what cool_dude wants - i.e. entering "1", "9", "5" to bring up 1959...

If you'll set the sorted property = true, then "195" will bring up "1950"

08-05-2005, 09:25 PM
If you'll set the sorted property = true, then "195" will bring up "1950"
Ah, thanks. And it resets to the beginning if you make a longe pause... guess that makes sense :)

08-05-2005, 09:29 PM
that works great!

thanks for your effort guys :cool:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum