Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Combo Box related......

Posted on 2004-11-18
14
Medium Priority
?
218 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:imarshad
14 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 12622322
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
 
LVL 13

Author Comment

by:imarshad
ID: 12622412
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
 

Expert Comment

by:iterat8d
ID: 12622464
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:dbrckovi
ID: 12622507
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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12622531
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
 
LVL 13

Author Comment

by:imarshad
ID: 12622778
>>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
 
LVL 11

Assisted Solution

by:dbrckovi
dbrckovi earned 2000 total points
ID: 12623621
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
 
LVL 11

Accepted Solution

by:
dbrckovi earned 2000 total points
ID: 12623969
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
 
LVL 13

Author Comment

by:imarshad
ID: 12624314
>>This will fix conflicts between mouse and typing

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

Imran
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12628772
>> 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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12628777
... using keyboard.

(forgot to finish the sentence)
0
 
LVL 13

Author Comment

by:imarshad
ID: 12723562
Sorry about the delay..... I thought I had accepted the answer......

Imran
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12723661
Thanks for A grade!
0
 
LVL 13

Author Comment

by:imarshad
ID: 12723682
You deserved it here....

Imran
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question