Another possibility is to create a custom GroupBox
, i.e. deriving from GroupBox
and exposing the event yourself:
Public Class CheckboxGroup
Inherits GroupBox
Public Event CheckboxChanged(source As CheckBox, e As EventArgs)
Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
' this method is called everytime a checkbox is added
If TypeOf e.Control Is CheckBox Then
Dim chk As CheckBox = DirectCast(e.Control, CheckBox)
AddHandler chk.CheckedChanged, AddressOf AllCheckedChange
End If
End Sub
Private Sub AllCheckedChange(source As Object, e As EventArgs)
If TypeOf source Is CheckBox Then
Dim chk As CheckBox = DirectCast(source, CheckBox)
RaiseEvent CheckboxChanged(chk, e)
End If
End Sub
End Class
You can then attach to the event in the Form
like:
Private Sub CheckboxChanged(source As CheckBox, e As EventArgs) Handles gb.CheckboxChanged
MsgBox(source.Text & " to " & source.Checked)
End Sub
Advantage: you can never miss to add an event handler to a CheckBox
, even if it is created dynamically.