VB.NET中的List(Of Event)

时间:2014-02-16 01:35:31

标签: vb.net winforms events delegates control-array

以下是较大项目的一部分,但就本问题而言,我有以下代码:

Public MustInherit Class Class1(Of T As {System.Windows.Forms.Control, New})
Inherits System.Windows.Forms.UserControl

Friend Items As New Dictionary(Of Integer, T)

    Sub Add(ByRef Item As T, ByVal Index As Integer)
        Me.Items.Add(Index, Item)
        AddHandler Item.Click, AddressOf Class1Click
    End Sub

    Public Shadows Event Click(ByVal sender As System.Object, ByVal e As System.EventArgs, ByVal Index As Integer)

    Sub Class1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        RaiseEvent Click(sender, e, DirectCast(sender, T).Index)
    End Sub

End Class

Public Class Class1CheckBox
    Inherits Class1(Of System.Windows.Forms.CheckBox)
End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    ...
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.MyClass1 = New Class1CheckBox()
        Me.CheckBox1 = New System.Windows.Forms.CheckBox()
        Me.CheckBox2 = New System.Windows.Forms.CheckBox()
        Me.CheckBox3 = New System.Windows.Forms.CheckBox()
        Me.CheckBox4 = New System.Windows.Forms.CheckBox()
        Me.SuspendLayout()
        ...
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox2.Name = "CheckBox2"
        Me.CheckBox3.Name = "CheckBox3"
        Me.CheckBox4.Name = "CheckBox4"
        ...
    End Sub
    ...
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBox3 As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBox4 As System.Windows.Forms.CheckBox
    Friend WithEvents MyClass1 As Class1CheckBox

End Class

Public Class Form1
    Private Sub MyClass1_Click(ByVal sender As Object, ByVal e As System.EventArgs, ByVal Index As Integer) Handles MyClass1.Click
        MessageBox.Show(DirectCast(sender, CheckBox).Name)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.OfType(Of CheckBox).AsParallel.ForAll(Sub(n) Me.MyClass1.Add(n, n.Index))
    End Sub

End Class

以上代码效果很好。只要单击四个复选框中的一个,单击偶数就会被MyClass1拦截并由MyClass1处理。这就是我想要的。

问题是“点击”是硬编码的。请注意,Class1是通用的。我希望它能够接受任何继承System.Windows.Forms.Control的类。某些控件可能包含Check事件,悬停或GotFocus。我需要的是如下内容,我只是不确定正确的语法是什么:

Public Class Class1CheckBox
    Inherits Class1(Of System.Windows.Forms.CheckBox)
    MyBase.AddEvent("Hover", <signature>...)
End Class

Public MustInherit Class Class1(Of T As {System.Windows.Forms.Control, New})
    Inherits System.Windows.Forms.UserControl

    Friend Items As New Dictionary(Of Integer, T)
    Friend Events As New List(Of Event)

    Sub AddEvent(EventName As String, ...)
        Events.Add(EventName...)
    End Sub

    Sub Add(ByRef Item As T, ByVal Index As Integer)
        Me.Items.Add(Index, Item)
        For Each MyEvent As Event In Events
            AddHandler ...
        Next MyEvent
    End Sub

    'Public Shadows Event Click(ByVal sender As System.Object, ByVal e As System.EventArgs, ByVal Index As Integer)

    'Sub Class1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    RaiseEvent Click(sender, e, DirectCast(sender, T).Index)
    'End Sub

End Class

创建某种事件序列的正确语法是什么,以及如何能够引发这些事件?

谢谢,

2 个答案:

答案 0 :(得分:0)

你想要的是不可能的。事件是成员,就像方法和属性一样。除非您知道您拥有该成员的类型,否则您无法编写代码来访问任何成员。

与方法和属性一样,如果要确定在运行时使用哪个事件,则必须使用Reflection。

答案 1 :(得分:0)

正如jmcilhinney所说,你需要使用反射。

这是一个简单的例子:

Imports System.Reflection

Public MustInherit Class Class1(Of T As {Control, New})

    Private _Items As New Dictionary(Of Integer, T)
    Private _Events As New Dictionary(Of String, [Event])

    Protected Sub AddEvent(eventName As String, [delegate] As [Delegate])
        If (Not Me._Events.ContainsKey(eventName)) Then
            Dim info As EventInfo = GetType(T).GetEvent(eventName)
            If (info Is Nothing) Then
                Throw New ArgumentOutOfRangeException("eventName")
            End If
            Me._Events.Add(eventName, New [Event]([delegate], info))
        End If
    End Sub

    Public Sub AddItem(item As T, index As Integer)
        Me._Items.Add(index, item)
        For Each [event] As KeyValuePair(Of String, [Event]) In Me._Events
            [event].Value.Info.AddEventHandler(item, [event].Value.Delegate)
        Next
    End Sub

    Friend Class [Event]
        Friend Sub New([Delegate] As [Delegate], Info As EventInfo)
            Me.[Delegate] = [Delegate]
            Me.Info = Info
        End Sub
        Public ReadOnly [Delegate] As [Delegate]
        Public ReadOnly Info As EventInfo
    End Class

End Class

Public Class Class1CheckBox
    Inherits Class1(Of CheckBox)

    Public Sub New()
        Me.AddEvent("CheckedChanged", New EventHandler(Sub(sender As Object, e As EventArgs) MsgBox(DirectCast(sender, CheckBox).Name & " is checked: " & DirectCast(sender, CheckBox).Checked.ToString())))
    End Sub

End Class