Share via


How to trigger CheckedChanged Event for Programmatically Created CheckBoxes

Question

Tuesday, September 12, 2017 8:27 PM

Hi,

I programmatically created CheckBoxes for my Windows Form app to represent COM Serial Ports present in a desktop computer (Windows 10).  I didn't want to manually create it in the Designer because I simply wanted to call SerialPort.GetPortNames and then programmatically create corresponding checkboxes equal to the number of available com ports.  What we need is for user to be able to check multiple checkboxes.

But now, how do I trigger the checked events for each CheckBox?

Appreciate any suggestions.

Marilyn Gambone

All replies (5)

Thursday, September 14, 2017 2:00 AM ✅Answered

Hi Marilyn,

You can not use "+=" to call event in VB.NET, "+=" is the syntax of C#, AddHandler is the VB.NET equivalent to C#’s "+=" when used on events.

So please refer to the code with VB.NET as below:

Public Class Form1
    Private CheckBoxes As New List(Of CheckBox)()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateCheckBoxes()
    End Sub
    Private Sub CreateCheckBoxes()
        Dim intialTop As Integer = 50
        For i As Integer = 0 To 3
            Dim chk As New CheckBox()
            chk.Top = intialTop
            chk.Left = 50
            chk.Text = "CheckBox Test"
            chk.Name = "chkTest"
            AddHandler chk.CheckedChanged, AddressOf ck_CheckedChanged
            Me.Controls.Add(chk)
            CheckBoxes.Add(chk)
            intialTop += 20
        Next
    End Sub
    Private Sub ck_CheckedChanged(sender As Object, e As EventArgs)
        MessageBox.Show("CheckBox was clicked!!!")
    End Sub
End Class

Regards,

Stanly

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, September 13, 2017 6:37 AM

Hi Marilyn,

>> how do I trigger the checked events for each Checkbox?

The CheckedChanged event is raised when the value of the Checked property changes between post to the server.

You could refer to the complete code as below:

        public Form1()
        {
            InitializeComponent();
        }
        List<CheckBox> CheckBoxes = new List<CheckBox>();
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateCheckBoxes();
        }
        private void CreateCheckBoxes()
        {
            int intialTop = 50;
            //string[] ports = SerialPort.GetPortNames();
            for (int i = 0; i < 4; i++)
            {
                CheckBox chk = new CheckBox();
                chk.Top = intialTop;
                chk.Left = 50;
                chk.Text = "CheckBox Test";
                chk.Name = "chkTest";
                chk.CheckedChanged += new EventHandler(ck_CheckedChanged);
                this.Controls.Add(chk);
                CheckBoxes.Add(chk);
 
                intialTop += 20;
            }
        }
        void ck_CheckedChanged(object sender, EventArgs e)
        {
            MessageBox.Show("CheckBox was clicked!!!");
        }

For more details about this event, please refer to the following document:

CheckBox.CheckedChanged Event.

Regards,

Stanly

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, September 13, 2017 5:10 PM

Hi,

I'm using VB.Net.  I follow the same loop statement but I'm getting an error on:

chk.CheckedChanged += New EventHandler(ck_CheckedChanged)

Error on chk.CheckedChanged: 

'Public Event CheckedChanged As EventHandler' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.'

Error on (ck_CheckedChanged): 'Delegate 'EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.

Marilyn Gambone


Wednesday, September 13, 2017 6:40 PM

You can omit the New EventHandler and just assigne the Method as long as its Declaration fits the EventHandler expected (probably CHeckedCHangedEventHandler(object sender, CheckChangedEventArgs e))

Please be so kind to close your Threads when you found an answer, these Threads should help everyone with similar issues.
You can close a Thread via the"Mark as Answer" link below posts. You can mark your own posts as answers if you were not helped out but found a solution, in such a case, please provide the answer.
Happy coding
PS: I assure everyone that I did not ever had the desire to offend anyone.


Thursday, September 14, 2017 12:45 PM

Worked Perfectly!!! Thank you.

Marilyn Gambone