在ListBox的DoubleClick方法中调用Click方法

时间:2017-04-13 07:31:43

标签: vb.net winforms reflection listbox

在为doubleclick执行特定代码后,我正在尝试调用一组列表框的click方法。


Private Sub ListBox1_Click(sender As Object, e As System.EventArgs) Handles ListBox1.Click
    ' Do something 1
End Sub

Private Sub ListBox2_Click(sender As Object, e As System.EventArgs) Handles ListBox2.Click
    ' Do something 2
End Sub

Private Sub ListBoxes_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick,
                                                                                                         ListBox2.DoubleClick
    ' Do something for doubleclick

    ' Invoke Click method
    Try
        Dim result As Object = Me.GetType().InvokeMember(sender.name + "_Click",
                                                         BindingFlags.Default Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod,
                                                         Nothing, Nothing, New Object() {sender, Nothing})
    Catch ex As Exception
    End Try

End Sub

结果是“MissingMethodException”异常

Method MyProject1.Form1.ListBox1_Click not found

但它就在那里,看看vb,它就在那里,在顶部,你不能看到吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

好吧,我对你的问题没有答案,但是经过一些返工,你就会到达某个地方。

Actuallu,你不想举办活动。你想调用正确的逻辑。

所以这就是我解决这个问题的方法(在C#中):

    private void listBox1_Click(object sender, EventArgs e)
    {
        DoSomething1();
    }

    private void DoSomething1()
    {
        throw new NotImplementedException();
    }

    private void listBox2_Click(object sender, EventArgs e)
    {
        DoSomething2();
    }

    private void DoSomething2()
    {
        throw new NotImplementedException();
    }

    private void listBox_DoubleClick(object sender, EventArgs e)
    {
        var listbox = sender as ListBox;

        // some logic to find out which method to call here...
        // DoSomething1();
        // or 
        // DoSomething2()
    }

答案 1 :(得分:0)

最终结果,基于Natrium的评论:

Private Sub ListBox1_Click(sender As Object, e As System.EventArgs) Handles ListBox1.Click
    ListBox1Process()
End Sub

Private Sub ListBox2_Click(sender As Object, e As System.EventArgs) Handles ListBox2.Click
    ListBox2Process()
End Sub

Private Sub ListBox1Process()
    ' Do something 1
End Sub

Private Sub ListBox2Process()
    ' Do something 2
End Sub

Private Sub ListBoxes_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick,
                                                                                                      ListBox2.DoubleClick
    ' Do something for doubleclick

    ' Invoke "Click" method
    Try
        Dim result As Object = Me.GetType().InvokeMember(sender.name + "Process",
                                                         BindingFlags.Default Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod,
                                                         Nothing, Me, Nothing)
    Catch ex As Exception
    End Try

End Sub