jeudi 7 septembre 2017

trying to assign a checkedChanged event handler with variable Checkbox name

I am using C# and adding checkboxes programmatically to a windows form. I am trying to assign a checkedChanged event handler to each checkbox created. Is there a way to use a varable checkbox name in the following case statement?

CheckBox chkBox = new CheckBox();
chkBox.Location = new System.Drawing.Point(550, y2);
chkBox.Name = "CheckBox" + optno.ToString();
chkBox.Font = new Font("Arial", 10, FontStyle.Bold);
switch (optno)
{
   case 1:
      chkBox.Click += new System.EventHandler(this.**CheckBox1**_CheckedChanged);
      break;
   case 2:
      chkBox.Click += new System.EventHandler(this.CheckBox2_CheckedChanged);
      break;
   case 3:
      chkBox.Click += new System.EventHandler(this.CheckBox3_CheckedChanged);
      break;

I want to avoid a long list of cases.




Aucun commentaire:

Enregistrer un commentaire