Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default listbox, topindex, and an extra click event

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,mous eup.

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,chan ge,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...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default listbox, topindex, and an extra click event

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
"Pointrider" wrote in message
...
|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,mous eup.
|
| 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,chan ge,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...


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default listbox, topindex, and an extra click event

Jim Rech wrote:
[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...
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListBox Click Event BHatMJ Excel Discussion (Misc queries) 6 June 21st 07 09:34 PM
listbox click event help Michael Malinsky Excel Programming 4 December 23rd 05 09:27 PM
Click event on listbox IanC Excel Programming 2 April 5th 05 07:05 AM
click event on listbox CStephenson Excel Programming 1 March 22nd 05 06:21 PM
Is refreshing listbox rowsource in listbox click event possible? Jeremy Gollehon[_2_] Excel Programming 4 September 25th 03 06:45 PM


All times are GMT +1. The time now is 06:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"