tags:

views:

217

answers:

3

In VB6, if a listbox containing alphanumeric data has focus and a character key is pressed, the first element in the listbox starting with that character is highlighted. If multiple characters are pressed, the first element starting with each character is selected after each character press. Typing M-A-R-T will select the first M-word, then the first A-word, etc.

What I want to do is write an algorithm that dynamically searches the listbox using a multiple character string. So typing M-A-R-T will highlight the first element beginning with M-A-R-T. The "Sorted" property already does this, but my listboxes are in a wrapper that uses a custom sorting method that gets broken if Sorted is turned on.

I've written all the code to search the listbox and it works correctly, except that the default search behavior is still happening. When I press M, the first M word is highlighted. Then I press A, and the first A word is highlighted. When I release A, the first M-A word is highlighted. Then I press R, and the first R word is highlighted. Then I release R and the first M-A-R word is highlighted. So the behavior is what I want, except there is an extra search being done somewhere between the Keydown and Keyup events.

Is there a way to disable or mask the default listbox searching behavior? Or a way to lock the scrollbar so the system can't scroll it?

A: 

The automatic scrolling was happening sometime after the KeyDown event. If a form element is disabled, it won't register any key events. So I added these 3 lines after my own filter:

    mobjListBox.Enabled = False
    mobjListBox.Enabled = True
    mobjListBox.SetFocus

I guess when a key is first pressed, VB6 figures out all of the key events it is going to call ahead of time. If the control is disabled, VB refactors the list of key events to call. So by disabling the control I force VB to remove the other Key events (like Scroll) from the workflow. Then I enable the control again and give it back the focus.

Tadaa!

Bobwise
+1  A: 

Can you please provide me the code as I can understand it better ?

tushar
+1  A: 

Try adding this:

Private Sub List1_KeyPress(KeyAscii As Integer)
    KeyAscii = 0
End Sub
dummy