将动态创建的事件处理程序添加到窗口窗体

时间:2014-07-11 18:11:36

标签: c# asp.net window

我有一个窗口表单,我在其中创建一个Checkbox列表。创建的复选框数量取决于从数据库返回的项目数。我已经能够创建复选框;但是,我不知道如何为这些复选框添加事件处理程序。例如,我想添加一个OnCheckedChanged或CheckStateChanged事件。我该如何添加这些活动?此外,我将不胜感激任何其他建议。我是编程的新手。

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]);
            }

        }

2 个答案:

答案 0 :(得分: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 }

注意:这将为您的所有复选框提供相同的事件处理。 如果要为不同的文本框执行不同的操作,则必须以不同的方式命名事件处理程序并定义该事件处理程序。

设置复选框位置的更有效方法

    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]);
    }

答案 1 :(得分:1)

        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());
        }
    }