Adding Event Handler for Dynamically Created to wi

2019-08-30 06:27发布

问题:

I have a window form where I am creating a list of Checkboxes. The number of checkboxes created are based upon how many items are returned from the database. I've been able to create the checkboxes; however, I am not sure how to add event handlers for these checkboxes. For example, I'd like to add an OnCheckedChanged or CheckStateChanged event. How can I add these events? Also, I would appreciate any other suggestion. I am a total newbie to programming.

private void Form1_Load(object sender, EventArgs e)
        {
            CheckBoxes = new CheckBox[listGroup.Count()];
            for (int i = 0; i < listGroup.Count(); i++)
            {
                CheckBoxes[i] = new CheckBox();
                CheckBoxes[i].Text = listGroup.ElementAt(i).GroupName;
                CheckBoxes[i].Name = "txt" + listGroup.ElementAt(i).GroupName.Replace(' ', '_');
                CheckBoxes[i].CheckedChanged += new EventHandler(CheckBoxes[i]+"_CheckedChanged");
                CheckBoxes[i].Width = 200;
                if (i == 0)
                {
                    CheckBoxes[i].Location = new System.Drawing.Point(5, 10);
                }
                else if (i == 1)
                {
                    CheckBoxes[i].Location = new System.Drawing.Point(5, 40);
                }
                else if (i == 2)
                {
                    CheckBoxes[i].Location = new System.Drawing.Point(5, 80);
                }
                this.Controls.Add(CheckBoxes[i]);
            }

        }

回答1:

private void Form1_Load(object sender, EventArgs e)
{
    //...
    CheckBoxes[i].CheckedChanged += checkBoxes_CheckedChanged;
    CheckBoxes[i].CheckStateChanged += checkBoxes_CheckStateChanged;
}

void checkBoxes_CheckedChanged(object sender, EventArgs e)
{ //do stuff when checked changed }

void checkBoxes_CheckStateChanged(object sender, EventArgs e)
{ //do stuff when check state changed }

Note: this will give identical event handling for all of your checkboxes. If you want to do different things for different textboxes, you have to name the eventhandler differently and define that eventhandler.

A more efficient way to set the location of your checkboxes

    for (int i = 0; i < listGroup.Count(); i++)
    {
        CheckBoxes[i] = new CheckBox();
        CheckBoxes[i].Text = listGroup.ElementAt(i).GroupName;
        CheckBoxes[i].Name = "txt" + listGroup.ElementAt(i).GroupName.Replace(' ', '_');
        CheckBoxes[i].CheckedChanged += new EventHandler(CheckBoxes[i] + "_CheckedChanged");
        CheckBoxes[i].Width = 200;
        //set location based on index of i
        CheckBoxes[i].Location = new System.Drawing.Point(5, 10 + (i * 30));
        this.Controls.Add(CheckBoxes[i]);
    }


回答2:

        private void LoadNewCheckboxes()
    {
        dynamic listGroupCount = 10;

        List<System.Windows.Forms.CheckBox> CheckBoxes = new List<System.Windows.Forms.CheckBox>();
        for (int i = 0; i <= listGroupCount - 1; i++)
        {
            System.Windows.Forms.CheckBox chkbox = new System.Windows.Forms.CheckBox();

            chkbox.Text = i.ToString();
            //listGroup.ElementAt(i).GroupName
            chkbox.Name = "txt" + i.ToString();
            //listGroup.ElementAt(i).GroupName.Replace(" "c, "_"c)
            chkbox.CheckedChanged += new EventHandler(chkbox_CheckedChanged);
            chkbox.CheckStateChanged += new EventHandler(chkbox_CheckStateChanged);
            chkbox.Width = 200;
            chkbox.AutoSize = true;

            this.Controls.Add(chkbox);
            CheckBoxes.Add(chkbox);

            if (i == 0)
            {
                chkbox.Location = new System.Drawing.Point(5, 10);
            }
            else
            {
                chkbox.Location = new System.Drawing.Point(5, (CheckBoxes[i - 1].Top + CheckBoxes[i - 1].Height + 10));
            }

        }
    }

    private void chkbox_CheckedChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.CheckBox chkbox = (System.Windows.Forms.CheckBox)sender;
        if (chkbox != null)
        {
            //do somthing
            Debug.WriteLine("chkbox_CheckedChanged");
            Debug.WriteLine(chkbox.Text);
            Debug.WriteLine(chkbox.Checked.ToString());
            Debug.WriteLine(chkbox.Name.ToString());
        }
    }

    private void chkbox_CheckStateChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.CheckBox chkbox = (System.Windows.Forms.CheckBox)sender;
        if (chkbox != null)
        {
            //do somthing
            Debug.WriteLine("chkbox_CheckStateChanged");
            Debug.WriteLine(chkbox.Text);
            Debug.WriteLine(chkbox.Checked.ToString());
            Debug.WriteLine(chkbox.Name.ToString());
        }
    }