0
votes

The following is part of a larger project, but for the purpose of this question, I have the following code:

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

The above code works beautifully. Anytime one of the four checkboxes are clicked, the click even is intercepted by the MyClass1 and is handled by MyClass1. That's what I want.

The problem is that "Click" is hardcoded. Notice, that Class1 is generic. I want for it to be able to accept any class which inherits System.Windows.Forms.Control. Some controls may have a Check event, or a hover, or a GotFocus. What I need is something as follows, I'm just not sure what the proper syntax is:

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

What is the proper syntax to create some kind of sequence of events, and how would I be able to raise those events?

Thanks,

2

2 Answers

0
votes

What you want is not possible. Events are members, just like methods and properties. You can't write code to access any member unless you know that the type you have has that member.

Just as with methods and properties, if you want to decide what event to use at run time then you have to use Reflection.

0
votes

As jmcilhinney correctly say, you need to use reflection.

Here is a simple example:

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