In this article I will explain with an example, how to assign dynamic CheckChanged event to CheckBox in Windows Forms (WinForms) Application using C# and VB.Net.
 
Database
This article makes use of a table named Fruits whose schema is defined as follows.
Assign dynamic CheckChanged event to CheckBox in Windows Application using C# and VB.Net
 
The Fruits table has the following records.
Assign dynamic CheckChanged event to CheckBox in Windows Application using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Form Design
The Form consists of a FlowPanelLayout. The dynamic CheckBoxes will be added to the FlowLayoutPanel control.
Assign dynamic CheckChanged event to CheckBox in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data;
Imports System.Data.SqlClient;
 
 
Assign dynamic CheckChanged event to CheckBox in Windows Application
Inside the Form Load event handler, first the list of Fruits is fetched from the Database Table and then a loop is executed over the list of Fruits.
Inside the loop, CheckBox control is dynamically created and it is assigned with a CheckChanged event handler.
Finally dynamically created CheckBox is added to the FlowLayoutPanel control.
Inside the CheckChanged event handler, first the checked or unchecked CheckBox is referenced and if the CheckBox is checked, its Text is shown using Message Box.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.PopulateCheckBoxes();
}
 
private void PopulateCheckBoxes()
{
    string query = "SELECT FruitName FROM Fruits";
    string constr = @"Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
        {
            //Fill the DataTable with records from Table.
            DataTable dt = new DataTable();
            sda.Fill(dt);
 
            //Loop and add CheckBoxes to FloyLayoutPanel.
            foreach (DataRow row in dt.Rows)
            {
                CheckBox chk = new CheckBox();
                chk.Width = 80;
                chk.Text = row["FruitName"].ToString();
                chk.CheckedChanged += new EventHandler(CheckBox_Checked);
                checkBoxPanel.Controls.Add(chk);
            }
        }
    }
}
 
private void CheckBox_Checked(object sender, EventArgs e)
{
    CheckBox chk = (sender as CheckBox);
    if (chk.Checked)
    {
        MessageBox.Show("You selected: " + chk.Text);
    }
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.PopulateCheckBoxes()
End Sub
 
Private Sub PopulateCheckBoxes()
    Dim query As String = "SELECT FruitName FROM Fruits"
    Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true"
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
            For Each row As DataRow In dt.Rows
                Dim chk As CheckBox = New CheckBox()
                chk.Width = 80
                chk.Text = row("FruitName").ToString()
                AddHandler chk.CheckedChanged, AddressOf CheckBox_Checked
                checkBoxPanel.Controls.Add(chk)
            Next
        End Using
    End Using
End Sub
 
Private Sub CheckBox_Checked(ByVal sender As Object, ByVal e As EventArgs)
    Dim chk As CheckBox = (TryCast(sender, CheckBox))
    If chk.Checked Then
        MessageBox.Show("You selected: " & chk.Text)
    End If
End Sub
 
 
Screenshot
Assign dynamic CheckChanged event to CheckBox in Windows Application using C# and VB.Net
 
 
Downloads