tags:

views:

1608

answers:

2

I have a need to run different code when a user clicks or double-clicks an item in a VB6 ListBox control. When I click on the control, the Click event handler executes. However, I am finding that when I double-click on the control, both the Click and DblClick event handlers execute.

Anyone have a good solution for getting just the DblClick event handler code to run without the Click code being executed first?

Thanks in advance for any suggestions.

+2  A: 

A slight hack, but you could use a Timer control and a boolean variable:

  • Start the timer on the Click event, set the boolean variable to false
  • Set the boolean variable to true on the DoubleClick event
  • When the timer Tick event fires, check the boolean variable to see if the user did a Click or DoubleClick

I'd recommend setting the timer interval to the Windows double click time setting, plus a bit (There should be a Windows API call that will give you this value).

Richard Ev
Beat me to it! I've used the same technique in the past.
DJ
I had to type quickly! ;-)
Richard Ev
The "official" answer from MS; same concept:http://support.microsoft.com/?kbid=109865
E Brown
A: 

The behavior of the list control in Visual Basic 6 is as follows.

1) When you click on an item the click event is fired. 2) When you click on the SAME item the click event is still fired. 3) When you double click on the item both the click event and double click event are fired.

Note #2. What you need to do is have a currentitem in the form you working with. Everytime the click event is checked you first see if the current item is the same item as what you clicked. If it is NOT the same item you process the click event normally.

If you double click on a NEW Item then the user is doing two things at the same time. Selecting a new item and performing the double click item. In this case the click is fired first then the double click.

This sequence is no different than if the control did what you wanted it to. It is the same as if you clicked on a new item wait X second then double click the same item.

If your click event involves handing off the task to a ActiveX EXE and immediately terminating or setting data to be processed later by a background timer. Then you need to have a semaphore or a flag to indicate when the process spawned by the click event is done. The Double Click event won't be processed until the semaphore is cleared.

If you have additional details my answer can be more specific. The following is some code you can run to see the List Box in actions. Create a Form and add three items to the list. Then add the below code.

Private Sub List1_Click()
    Print "Clicked - " & Me.List1.List(Me.List1.ListIndex)
End Sub

Private Sub List1_DblClick()
    Print "Double Clicked - " & Me.List1.List(Me.List1.ListIndex)
End Sub

Do the following steps

  1. Click on Item #1
  2. Click on Item #2
  3. Click on Item #2 again
  4. Double Click on Item #2
  5. Double Click on Item #3

You will see the various combination of actions occuring.

RS Conley