Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does Setting ListBox.ListIndex Invoke Listbox_Click Event?

Status
Not open for further replies.

f64

Technical User
May 23, 2003
24
US
Does setting the ListBox.ListIndex value automatically invoke the Listbox_Click event? In a section of code outside of the ListBox_Click event I am setting the value of ListBox.ListIndex. After this is executed, changes are made to the ListBox indicating the ListBox_Click event had been invoked. Does setting the ListIndex number also run ListBox_Click? If so, why?

 
For native Access listbox controls it does, so there something like this

[tt]me!lstMyList.selected(N) = True[/tt]

can be used in stead (N - index of the item you wish to select) - I think the same syntax should work also for MSForms listbox controls.

Roy-Vidar
 
f64,

Yes, it appears to be a side-effect. If you (or rather your code) set the ListIndex to the same value as it is currently, the Click event is not triggered.

I usually handle these situations using a boolean flag that is checked on entry to an event handler where I have code. If this boolean is set, I exit the handler. Example (say in a Userform module):
Code:
Dim IgnoreEvent As Boolean

Private Sub MySub()
' This sub will change ListBox.ListIndex

  IgnoreEvent = True
  ...
  ListBox.ListIndex = 3
  ...
  IgnoreEvent = False
End Sub

Private Sub ListBox_Click()
  If IgnoreEvent Then Exit Sub
End Sub

The net effect is that any additional code in the Click handler will not run as a result of other code changing the ListIndex property.

HTH
Mike


 
Yes, it does. Not only that but it fires the _Change event as well, in fact first.

For example:
Code:
Private Sub UserForm_Initialize()
ComboBox1.AddItem "zero"
ComboBox1.AddItem "one"
ComboBox1.AddItem "two"
ComboBox1.AddItem "three"
ComboBox1.AddItem "four"
ComboBox1.AddItem "five"
ComboBox1.ListIndex = 0
End Sub

fires Combobox1_Change, THEN
fires Combobox1_Click, THEN
finally display the UserForm

You can easily see this order by testing with a command button that:
Code:
Private Sub CommandButton1_Click()
' make a change to the ListIndex
ComboBox1.ListIndex = InputBox("index number?")
End Sub

' now make so you get DIFFERENT result for _Change and _Click

Private Sub ComboBox1_Change()
MsgBox "Listindex is " & ComboBox1.ListIndex
End Sub

Private Sub ComboBox1_Click()
MsgBox "Listindex is " & ComboBox1.ListIndex + 1
End Sub

Now I understand why the _Change event fires. After all, the state of the combobox IS changed. I am not why it is required to fire _Click - as the control is NOT clicked.

You can worked around this by making a global variable that is set in the _Change event, and checked in the _Click event. That way, if you do not want both to fire you can do so.

Gerry
 
Darn, some people are so FAST! Mike got in there saying essentially the same thing.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top