How to run the same code in a click event as a double click event?

I have 4 items in a ListBox, and each item does a certain thing when it is clicked.

But I also want the double-click event to do the same thing as the click event.

I can copy and paste all the code from the click event into a double-click event, but then you have a lot of code pushing the code page while doing the same. So what to do about it?

Example:

Private Sub listBox1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) _ Handles listBox1.DoubleClick if listbox1.doubleclick then do the same thing in listbox1.clickevent end if End Sub 
+1
source share
3 answers

Try entering the code!

 Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click ListBox1_DoubleClick(sender, e) End Sub Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick MsgBox(ListBox1.Items.Count) End Sub 
+2
source

The same procedure can handle both events. The code:

 Private Sub ListBox1_AllClicks( ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click, ListBox1.DoubleClick 

You can set this in the list properties: view events and use the drop-down list next to DoubleClick to select an existing procedure.

+3
source

These solutions only work if the executable code is accessible to the class (class A), which may not be the case if you are creating a library. If the control (s) that must execute the same code is in a class that will be created in another class (class B), and the instatiating class is the one where the code to be executed will be defined, then you can do this

Class A

 Public Event ListDoubleClickOrClick() Private Sub HandleListDoubleClickOrClick(sender As Object, e As System.EventArgs) Handles listObject.DoubleClick, listObject.Click RaiseEvent ListDoubleClickOrClick() End Sub 

Class B

  private sub theCodeToBeExecuted() end sub dim objClassA as A AddHandler objClassA.ListDoubleClickOrClick, AddressOf theCodeToBeExecuted 
0
source

All Articles