Combo Box related......

Hi all,
          I have a Combo Box in my form.... The data in the combo box is something like this...
92-1001
92-1002
92-1005
92-1010
92-1015
92-2000
and so on almost about 200 entries......
This combo box is used to select one of these and I have written all my code in Combo1_click event......Now I do the Selection by scrolling down the combo box until I reach the desired one. This is difficult and takes some time to reach the desired one now but the entries in this combo box are expected to be somewhere round about 1500-1600.....and it will be really difficult to scroll 1500 entries.....

What I want is that if I write 1015 while the Combo Box is selected the combo box should automatically scroll down to that entry......(Something in the Combo1_keypress event maybe)

Also If I write 2 it should jump to the 92-2000 entry automatically..........

Note that 92- will always remain the same for all my entries....

Imran
LVL 13
imarshadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkaiosIT DirectorCommented:
Correct me if I'm wrong, but when you press the F4 key to display the drop down and you try to enter a matching entry, it should automatically scroll down to that one.
0
imarshadAuthor Commented:
My Combo Box is having Style set to "Drop Down List" and in this mode it doesnot scroll to it (as far as my experience is concerned) In my experience if I type anything other then 9 then It doesnot scroll but upon typing 9 it scrolls to the the next entry...... While I just want to get rid of the first three characters i.e the User shouldnot type 92-2 to reach the value but just typing 2 should reach him to 92-2000 and if he writes something more i.e 2 and then 1 ,then it should scroll to 92-2100 and similarly if he types 2106 it should scroll to 92-2106.......

Imran
0
iterat8dCommented:
If the combo box is always going to have "92-xxxx" in it... then you should be able to move the 92- part into a label to the left of the combo box. Then the mouse wheel, or arrow keys, would scroll the box when selected (has focus).

If the combo box was the standard type you could just type in the box, as it's an editable control in that mode, and automatic selection will then work if you type "1012" it will move to that point.

Leaving it as a drop down list limits the options for typing into it.

Good suggestion about using F4. In the mode currently selected F4 just opens the box... and won't scroll as the numbers are typed; normally it would scroll after pressing F4 and that would be a smooth solution.

If would work better, to help select the numbers with the keyboard/pad, if the combo box was in the drop down (0) mode.

Hope that helps.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

dbrckoviCommented:
Hi!

Try this:

'------------------------------------------------
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyBack And Len(Combo1.Text) <= 3 Then
        KeyCode = 0
        Combo1.Text = "92-"
        Combo1.SelStart = 3
        Exit Sub
    End If
   
    If Left(Combo1.Text, 3) = "92-" Then
    Else
        Combo1.Text = "92-" & Combo1.Text
        Combo1.SelStart = Len(Combo1.Text)
    End If
End Sub
'----------------------------------------------------------
0
dbrckoviCommented:
BTW. Style should be DropDown Combo.

If I'm not mistaking, difference between DropDown Combo and DropDown List is that you can't type to Dropdown List.
But since you need to be able to type to it, use DropDown Combo Style.
0
imarshadAuthor Commented:
>>If I'm not mistaking, difference between DropDown Combo and DropDown List is that you can't type to Dropdown List.

Yes this is true but still the Combo1_KeyUp and Combo1_KeyPress events are fired and I can get the ascii value of the button pressed....... I was looking for a routine that should be inserted into the Combo1_KeyUp event that will scroll the Combo according to the key/key combination pressed......

Imran
0
dbrckoviCommented:
OK. Try this.

'------------------------------------------------------------------
Private Sub Combo1_KeyPress(KeyAscii As Integer)
    Dim SearchText As String
    Dim Item As String
    Dim SearchStart As Integer
    Dim PreviousIndex As Integer
       
    PreviousIndex = Combo1.ListIndex
   
    If KeyAscii = vbKeyBack Or Len(Combo1.Tag) = 0 Then
        SearchStart = 0
    Else
        SearchStart = Combo1.ListIndex
    End If
   
    If KeyAscii = vbKeyBack Then
        If Len(Combo1.Tag) > 0 Then Combo1.Tag = Left(Combo1.Tag, Len(Combo1.Tag) - 1)
    Else
        Combo1.Tag = Combo1.Tag & Chr(KeyAscii)
    End If
   
    SearchText = "92-" & Combo1.Tag
   
    For x = SearchStart To Combo1.ListCount - 1
        Item = Combo1.List(x)
        If Left(Item, Len(SearchText)) = SearchText Then
            Combo1.ListIndex = x
            Exit For
        End If
    Next x
   
    If KeyAscii = vbKeyBack Then
    Else
        If Combo1.ListIndex = PreviousIndex And Combo1.ListIndex > 0 Then
            Combo1.Tag = Left(Combo1.Tag, Len(Combo1.Tag) - 1)
        End If
    End If
   
    KeyAscii = 0
End Sub

Private Sub Combo1_LostFocus()
    Combo1.Tag = ""
End Sub
'---------------------------------------------------------------------------------
0
dbrckoviCommented:
This will fix conflicts between mouse and typing, but since it's using SubClassing, allways save your project before running it.

 - paste this to form
'-----------------------------------------------------
Private Sub Combo1_GotFocus()
    WindowHook True
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
    Dim SearchText As String
    Dim Item As String
    Dim SearchStart As Integer
    Dim PreviousIndex As Integer
       
    PreviousIndex = Combo1.ListIndex
   
    If KeyAscii = vbKeyBack Or Len(Combo1.Tag) = 0 Then
        SearchStart = 0
    Else
        SearchStart = Combo1.ListIndex
    End If
   
    If KeyAscii = vbKeyBack Then
        If Len(Combo1.Tag) > 0 Then Combo1.Tag = Left(Combo1.Tag, Len(Combo1.Tag) - 1)
    Else
        Combo1.Tag = Combo1.Tag & Chr(KeyAscii)
    End If
   
    SearchText = "92-" & Combo1.Tag
   
    For x = SearchStart To Combo1.ListCount - 1
        Item = Combo1.List(x)
        If Left(Item, Len(SearchText)) = SearchText Then
            Combo1.ListIndex = x
            Exit For
        End If
    Next x
   
    If KeyAscii = vbKeyBack Then
    Else
        If Combo1.ListIndex = PreviousIndex And Combo1.ListIndex > 0 Then
            Combo1.Tag = Left(Combo1.Tag, Len(Combo1.Tag) - 1)
        End If
    End If
   
    KeyAscii = 0
End Sub

Private Sub Combo1_LostFocus()
    Combo1.Tag = ""
    WindowHook False
End Sub

Sub Combo1_MouseUp()
    Combo1.Tag = ""
End Sub
'---------------------------------------------------

 - creatre new module and paste this:
 - Don't forget to change Form1.Combo1  to names you use, becouse if you get an error in this module, there
   is a good chance that VB will crash or experience some weird behaviour errors
'-----------------------------------------------------
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const GWL_WNDPROC = (-4)
Private Const WM_LBUTTONUP = &H202

Private lPrevProc As Long

Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    On Error Resume Next
       
        WindowProc = CallWindowProc(lPrevProc, hwnd, uMsg, wParam, lParam)
        If uMsg = WM_LBUTTONUP Then Call Form1.Combo1_MouseUp
   
    If Err <> 0 Then Debug.Print "Error: " & Err.Number & " : " & Err.Description
End Function

Public Sub WindowHook(ByVal bHook As Boolean)
    If bHook = True Then
        lPrevProc = SetWindowLong(Form1.Combo1.hwnd, GWL_WNDPROC, AddressOf WindowProc)
    Else
        Call SetWindowLong(Form1.Combo1.hwnd, GWL_WNDPROC, lPrevProc)
    End If
End Sub
'------------------------------------------------------------------------------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
imarshadAuthor Commented:
>>This will fix conflicts between mouse and typing

What conflicts can occur? The first code is working fine for me.....

Imran
0
dbrckoviCommented:
>> What conflicts can occur?

If you select some item using mouse, then control will not update the text you entered.
For example, if you type 1032, then 92-1032 will be selected automaticaly, but if you now use mouse to select 2754, the control will still remember 1032.
Next time you type a number, it won't continue from 2754 as it should, but from 1032.

It could be fixed easily if Click event would behave as expected. We could just delete the remembered value in click event, and next time you woud try to type,
it would start from the beginning.
But since Click is fired every time when selection is changed, it would delete remembered text every time you type something to the box.

Try selecting something using first sollutoin first by typing, then by mouse, then by typing again. You'll see the conflict.
If you now put  Combo1.Tag = "" in control's click event, you would hardly be able to select anything.
0
dbrckoviCommented:
... using keyboard.

(forgot to finish the sentence)
0
imarshadAuthor Commented:
Sorry about the delay..... I thought I had accepted the answer......

Imran
0
dbrckoviCommented:
Thanks for A grade!
0
imarshadAuthor Commented:
You deserved it here....

Imran
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.