listbox, topindex, and an extra click event

P

Pointrider

I have a userform with a text box and a listbox. As the user types into
the text box, I use the textbox_change event to see if the partial entry
(I use LIKE and a wildcard) has a match in the listbox, and if so I use
the listindex and topindex properties to select the match and scroll it
to the top of the listbox. No problem yet.

The listbox items are of the form "lastname, firstname", so there will
be several matches as the first characters are typed. If the user sees
the wanted match selected in the listbox, then a tab will copy the match
to the text box and move to the next form control. I use the
textbox_exit event for this, and it works fine.

Since the topindex property has scrolled the listbox so the match is on
top, the user may see the entry they want just a few items down the
list. So, typing "joh" could result in a listbox with "johnson, ann" on
top, followed by "johnson, beth". If the user wants the second entry, I
want them to be able to double-click it and then move on.

My code for the listbox_dblclick event is simple...

Private Sub NamesListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
With NamesListBox
NameBox.Value = .Value
End With
End Sub

This too works fine.

Then I decided to reset the topindex with the double-click, to move the
selected item to the top of the listbox, and ran into trouble.

With the code above, the sequence of event triggers I see (for the
listbox) is:

enter,mousedown,change,click,mouseup,dblclick,mouseup.

If I reset the topindex property, the event code looks like

Private Sub NamesListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
With NamesListBox
.TopIndex = .ListIndex
NameBox.Value = .Value
End With
End Sub

When I execute this, the sequence of events is

enter,mousedown,change,click,mouseup,dblclick,change,click,mouseup

There is an extra Change and an extra Click! The result is that the
correct item is scrolled to the top if the listbox, but the highlighted
(selected) item becomes the one at the mouse position when the
double-click occurred.

Does anyone know what's going on? Why the extra event triggers if I set
the topindex? Can't find anything about this anywhere on the web or in
the newsgroups.

Thanks...
 
J

Jim Rech

While I don't have the time to recreate your exact scenario, reading about
it reminded me of some similar travails I've enjoyed. For the most part
I've found it advisable when working with events in userforms to turn off
(or at least control) events created by my own code. When the user does
something I want event code to run but when my code creates the same event I
find it generally advantageous to not allow the event code to run. Thus
avoiding massive recursion...

I turn off events in the userform with a module level boolean RunMode.
RunMode is true when the user does something but false when my code does
something. So..

Private Sub NamesListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If RunMode Then
RunMode = False
With NamesListBox
NameBox.Value = .Value
End With
RunMode = True
End If
End Sub

I have RunMode control just about every event in the userform and I find it
makes things much more manageable. Whether it can help in your situation
I'm not sure, so fwiw

--
Jim
|I have a userform with a text box and a listbox. As the user types into
| the text box, I use the textbox_change event to see if the partial entry
| (I use LIKE and a wildcard) has a match in the listbox, and if so I use
| the listindex and topindex properties to select the match and scroll it
| to the top of the listbox. No problem yet.
|
| The listbox items are of the form "lastname, firstname", so there will
| be several matches as the first characters are typed. If the user sees
| the wanted match selected in the listbox, then a tab will copy the match
| to the text box and move to the next form control. I use the
| textbox_exit event for this, and it works fine.
|
| Since the topindex property has scrolled the listbox so the match is on
| top, the user may see the entry they want just a few items down the
| list. So, typing "joh" could result in a listbox with "johnson, ann" on
| top, followed by "johnson, beth". If the user wants the second entry, I
| want them to be able to double-click it and then move on.
|
| My code for the listbox_dblclick event is simple...
|
| Private Sub NamesListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
| With NamesListBox
| NameBox.Value = .Value
| End With
| End Sub
|
| This too works fine.
|
| Then I decided to reset the topindex with the double-click, to move the
| selected item to the top of the listbox, and ran into trouble.
|
| With the code above, the sequence of event triggers I see (for the
| listbox) is:
|
| enter,mousedown,change,click,mouseup,dblclick,mouseup.
|
| If I reset the topindex property, the event code looks like
|
| Private Sub NamesListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
| With NamesListBox
| .TopIndex = .ListIndex
| NameBox.Value = .Value
| End With
| End Sub
|
| When I execute this, the sequence of events is
|
| enter,mousedown,change,click,mouseup,dblclick,change,click,mouseup
|
| There is an extra Change and an extra Click! The result is that the
| correct item is scrolled to the top if the listbox, but the highlighted
| (selected) item becomes the one at the mouse position when the
| double-click occurred.
|
| Does anyone know what's going on? Why the extra event triggers if I set
| the topindex? Can't find anything about this anywhere on the web or in
| the newsgroups.
|
| Thanks...
 
P

Pointrider

Jim said:
[snip]
For the most part
I've found it advisable when working with events in userforms to turn off
(or at least control) events created by my own code. When the user does
something I want event code to run but when my code creates the same event I
find it generally advantageous to not allow the event code to run. Thus
avoiding massive recursion...
[snip]

Thanks Jim, good idea... and it got me thinking about just paring this
problem to bare essentials. So now I have a userform with just a listbox
(sized to show only part of the list) and a control button, and the
following code:
--
Private Sub CommandButton1_Click()
MsgBox ListBox1.ListIndex 'just to look at the index
End Sub
--
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ListBox1.TopIndex = ListBox1.ListIndex
End Sub
--
Private Sub UserForm_Initialize()
ListBox1.AddItem "johnson, beth"
ListBox1.AddItem "johnson, jane"
ListBox1.AddItem "johnson, mary"
ListBox1.AddItem "smith, al"
ListBox1.AddItem "smith, bert"
ListBox1.AddItem "thomas, quincy"
ListBox1.AddItem "wilson, ann"
ListBox1.AddItem "young, tom"
ListBox1.AddItem "young, dick"
ListBox1.AddItem "young, harry"
End Sub
--

The behavior with this setup is exactly what I've been seeing in my full
application. If you double click on an entry toward the bottom of the
listbox window, that entry scrolls to the top of the window but the
selected item becomes the one at the position of the dblclick. This
happens because of the 'extra' click event that is generated. Seems like
a bug in VBA to me...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top