How do I create an eventhandler for 32 programmatically created checkboxes (winforms) ?

The code below is a working version of programmatically generated checkboxes in a FlowLayout Panel.

Question. How do I create an eventhandler for all of these 32 checkboxes (or checkboxes in a buttons' clothing) thx -greg

Public Class Form1
Dim iCamera As Short
Dim nCameras As Short = 32

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For iCamera = 0 To nCameras - 1
Dim c As New CheckBox
c.Appearance = Appearance.Button
c.MinimumSize = New Size(30, 23)
c.Width = Me.FlowLayoutPanel1.Width / nCameras
c.TextAlign = ContentAlignment.MiddleCenter
c.Text = iCamera.ToString()
c.FlatStyle = FlatStyle.System
c.FlatAppearance.MouseOverBackColor = Color.Blue
Me.FlowLayoutPanel1.Controls.Add(c)
Next
End Sub

End Class



Answer this question

How do I create an eventhandler for 32 programmatically created checkboxes (winforms) ?

  • leovernazza

    Here's an example using programatically creating textboxes and hooking up the controls to the same event handle programatically. In this example it creates some textboxes - you change the text in them and nothing happens except text changes - click the button to hookup the event handlers for these controls and then change the text in the same boxes and a messagebox will appear every time you change the text in one of the boxes.

    The bold italic text is the event handler and hookup code which can easily be applied to any other controls or event handlers.

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For i As Integer = 21 To 30
    Dim x As New TextBox
    x.Name = "TxtAdded" & i.ToString
    x.Top = i * 18
    x.Left = 30
    x.Visible = True
    Me.Controls.Add(x)
    Next
    End Sub

    Private Sub BtnAddHandler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each c As Control In Me.Controls
    If TypeOf (c) Is TextBox Then
    AddHandler c.TextChanged, AddressOf TextBox_TextChanged
    End If
    Next
    End Sub


    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    MsgBox(CType(sender, TextBox).Name)
    End Sub
    End Class


  • Quirk

    ahh I see... so the Addhandler creates this 'generic' textchanged handler for all textboxes created. And then you use the CType(sender to determine which one it is. Great! thank you! I'll give it a whirl. -g
  • yeos_lee

    Thank you. Would the Handles TextBox3.TextChanged need to be changed to include all (in this case) textboxes This seems to be hardcoded and assumes I would know ahead of time how many controls I created Handles textbox1.changed,textbox2.changed,textbox3.changed.....etc....
  • Krutika

    Sure the addhandler is the way to hookup controls to methods. As long as the signature on the method matches the signature of the event then you are good to go.

    So in this case I create a control and method for a textbox event to ensure that I got the correct method with signatures for the eventhandler - and then programatically hooked the controls to the method using eventhandler.

    The handles clause does pretty well the same thing but requires you to specify them at design time and not programatically at runtime.


  • ZeePrime

    You can remove the handles clause of the method - the only reason it was there was I used one of the controls of the type I wanted to hookup to ensure that the signature was correct.

     Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For i As Integer = 21 To 30
                Dim x As New TextBox
                x.Name = "TxtAdded" & i.ToString
                x.Top = i * 18
                x.Left = 30
                x.Visible = True
                Me.Controls.Add(x)
            Next
        End Sub

        Private Sub BtnAddHandler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each c As Control In Me.Controls
                If TypeOf (c) Is TextBox Then
                    AddHandler c.TextChanged, AddressOf TextBox_TextChanged
                End If
            Next
        End Sub


        Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            MsgBox(CType(sender, TextBox).Name)
        End Sub
    End Class


  • How do I create an eventhandler for 32 programmatically created checkboxes (winforms) ?