How to handle events of dynamically added checkbox on windows form

前端 未结 4 840
迷失自我
迷失自我 2021-01-27 02:58

I am able to add checkbox dynamically on windows form and add data value to its text property. On click of any checkbox I have run a procedure which will make certain other chec

相关标签:
4条回答
  • 2021-01-27 03:10

    Have you tried this

            CheckBox check = new CheckBox();
            check.Checked = true;
            check.AccessibleName = checkName;
            check.Location = new System.Drawing.Point(340, 40);
            check.CheckedChanged +=new EventHandler(check_CheckedChanged);
    
            this.Controls.Add(check);
    
    0 讨论(0)
  • 2021-01-27 03:15

    if the name of the dynamically added checkbox is c, the answer is as below:

    c.CheckedChanged += c_CheckedChanged;
    

    and c_CheckedChanged is as below:

     private void c_CheckedChanged(object sender, EventArgs e)
        {
           if (((CheckBox)sender).Checked)
           {
              ((CheckBox)(this.Controls.Find("c1", false))[0]).Enabled = false;
           }
        }
    

    which c1 is the name of the checkbox you want to disable.

    0 讨论(0)
  •   private void custom_event_handler(object sender, EventArgs e)
      {
           ....
      }
    

    and then you add checbox like this:

     CheckBox cb = new CheckBox();
     cb.CheckedChanged += new EventHandler(custom_event_hahndler);
    
    0 讨论(0)
  • 2021-01-27 03:26

    Add event handler when you create a checkbox programmatically. And its handler you can do your code logic.

    CheckBox dynamicCheckBox = new CheckBox();
    dynamicCheckBox.CheckedChanged +=new EventHandler(dynamicCheckBox_CheckedChanged);
    
    private void dynamicCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        // Your code
    }
    
    0 讨论(0)
提交回复
热议问题