简体   繁体   English

动态添加事件处理程序到窗口窗体

[英]Adding Event Handler for Dynamically Created to window Form

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. 例如,我想添加一个OnCheckedChanged或CheckStateChanged事件。 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]);
            }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM