vb.net如何动态点击事件

时间:2017-07-03 19:31:02

标签: vb.net events dynamic click devexpress

我试图通过点击其他控件来触发点击事件 使用以下代码:

Private Sub GroupControl4_Click(sender As Object, e As EventArgs) Handles GroupControl4.Click
    For Each c As Control In XtraTabControl1.Controls
        For Each cs As Control In DirectCast(c, DevExpress.XtraTab.XtraTabPage).Controls
            If cs.Tag = sender.Tag Then
                cs.PerformClick()
            End If
        Next
    Next
End Sub

我收到了这个错误:

  

"' PerformClick'不是' System.Windows.Forms.Control'"

的成员

我也尝试过DirectCast,我也没有帮助过。

1 个答案:

答案 0 :(得分:0)

这是解决方案

我正在创建一个新的功能

    Private Sub FireEvent(ByVal ctrl As Control, ByVal EventName As String)
    Dim flags As BindingFlags = BindingFlags.[Public] Or BindingFlags.NonPublic Or BindingFlags.[Static] Or BindingFlags.Instance Or BindingFlags.DeclaredOnly
    Dim EventP As PropertyInfo = Nothing
    Dim CtrlS As Object = ctrl.[GetType]
    Dim FoundEvents As Boolean = False
    While FoundEvents = False
        EventP = CtrlS.GetProperty("Events", flags)
        If EventP Is Nothing Then
            CtrlS = CtrlS.BaseType
        Else
            FoundEvents = True
        End If
    End While

    Dim EventF As FieldInfo = Nothing
    CtrlS = ctrl.[GetType]
    Dim FoundField As Boolean = False
    While FoundField = False
        EventF = CtrlS.GetField(EventName, flags)
        If EventF Is Nothing Then
            CtrlS = CtrlS.BaseType
        Else
            FoundField = True
        End If
    End While


    If Not EventP Is Nothing Then
        Dim eventH = DirectCast(EventP.GetValue(ctrl, Nothing), EventHandlerList)
        Dim eventFIeld As Object = EventF.GetValue(Nothing)
        Dim handlers As [Delegate] = eventH(eventFIeld)
        For Each handler As [Delegate] In handlers.GetInvocationList()
            handler.Method.Invoke(handler.Target, New Object() {ctrl, New EventArgs})
        Next
    End If

End Sub

并且此代码非常出色

Private Sub GroupControl4_Click(sender As Object, e As EventArgs) Handles GroupControl4.Click, GroupControl5.Click
    For Each c As Control In XtraTabControl1.Controls
        For Each cs As Control In DirectCast(c, DevExpress.XtraTab.XtraTabPage).Controls
            If cs.Tag = sender.Tag Then
                FireEvent(cs, "EventClick")
            End If
        Next
    Next
End Sub

感谢