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

后端 未结 3 505
小蘑菇
小蘑菇 2021-01-25 08:11

I have 4 items in a ListBox and each item does a specific thing when it gets clicked on.

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

相关标签:
3条回答
  • 2021-01-25 08:31

    These solutions only work if the code to be executed is available to the class (Class A), which may not be the case if you are building a library. If the control(s) which should execute the same code are in a class which is going to be instantiated in another class (Class B), and the instatiating class is 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 讨论(0)
  • 2021-01-25 08:36

    Try Following 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
    
    0 讨论(0)
  • 2021-01-25 08:44

    Same routine can handle both events. Code:

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

    You can set this up in the listbox properties: view events and use the dropdown next to DoubleClick to choose an existing routine.

    0 讨论(0)
提交回复
热议问题