The _Click event & Listindex property change 
Author Message
 The _Click event & Listindex property change

>When I change the Listindex property for a ListBox or a ComboBox, the
>associated _Click event is automatically performed.  Is there's a way to
>advoid this without putting extra variables in the _Click event code?

This is technically a bug in VB since VB1. However, many programmers started
using ListIndex to generate Click events in List and Combo boxes. Therefore,
it became a de facto "feature" because fixing it would break a lot of
programs. While it can be useful sometimes, it can also be a pain too.

There are two ways to work around this. One is to use some sort of flag
variable at the module level to prevent the click. The other is to use the
appropriate API calls to set the listindex without generating a click. Here
is some code I use to do this from my "common functions" module...

Private Const CB_SETCURSEL = &H14E
Private Const LB_SETCURSEL = &H18

Private Declare Function SendMessageByString Lib "user32" Alias
"SendMessageA" _
                                    (ByVal hwnd As Long, ByVal wMsg As Long,
                                     ByVal wParam As Long, ByVal lParam As
String) As Long

Sub SetListListIndex(lstList As ListBox , iIndex As Integer)
' Sets ListIndex (iIndex) in Listbox (lstList) *without* generating a Click
   Dim nRet As Variant

   nRet = SendMessageByString(lstList.hwnd, LB_SETCURSEL, iIndex, "")

End Sub

Sub SetComboListIndex(cboCombo As ComboBox , iIndex As Integer)
' Sets ListIndex (iIndex) in combobox (cboCombo) *without* generating a
Click event
   Dim nRet As Variant

   nRet = SendMessageByString(cboCombo.hwnd, CB_SETCURSEL, iIndex, "")

End Sub

Frank Carr

Sun, 01 Jul 2001 03:00:00 GMT  
 The _Click event & Listindex property change
I found the quickest way to solve this problem was just to use the mousedown
event insetead of the click event to activate a selection. Then if the click
event is called by changing the listindex you won't have a problem.


Wed, 04 Jul 2001 03:00:00 GMT  
 The _Click event & Listindex property change
Frank also gave you another idea, which is to use a module-level boolean to
detect whether the click is programmatic or not. That should be fine in a
multi-select situation.



>My problem is: When the ListIndex is changed PROGRAMMATICALLY , the _Click
>Event is executed.  Frank Carr gave me a good solution using the API calls,
>but it only works when the Multiselect property of the Control is set to

Fri, 06 Jul 2001 03:00:00 GMT  
 [ 8 post ] 

 Relevant Pages 

1. Resetting ListIndex Property Generates Click Event

2. Problem: Resetting ListIndex Property Causes Click Event

3. ListIndex generates a _Click event

4. Setting Combo box ListIndex initiates Combo box Click event

5. Listbox: Setting ListIndex is a Click Event?

6. Firing up the Click Event in a Combo when ListIndex is defined

7. ListIndex: Error 7777: You have used the ListIndex Property incorrectly

8. Changing properties of a Collection with a change event :/ <--- pls help

9. Changing properties of a Collection with a change event :/ <--- pls help

10. List Box - How to grab click event before selected property set

11. Using Selected property on Litsview Click Event

12. Trapping the Click event when the DBlist boundtext property is updated


Powered by phpBB® Forum Software