添加事件处理程序动态创建的,以窗口形式 [英] Adding Event Handler for Dynamically Created to window Form

查看:162
本文介绍了添加事件处理程序动态创建的,以窗口形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里,我创建一个复选框列表窗口的形式。创建复选框的数量是根据有多少项目是从数据库返回。我已经能够创建复选框;但是,我不知道如何添加事件处理这些复选框。例如,我想补充一个OnCheckedChanged或CheckStateChanged事件。如何添加这些事件?另外,我想AP preciate任何其他建议。我是一个新手总编程。

 私人无效Form1_Load的(对象发件人,EventArgs的发送)
        {
            的CheckBox =新的复选框[listGroup.Count()];
            的for(int i = 0; I< listGroup.Count();我++)
            {
                的CheckBox [I] =新的复选框();
                的CheckBox [I]。文本= listGroup.ElementAt(我).GroupName;
                的CheckBox [I] .Name点=TXT+ listGroup.ElementAt(I).GroupName.Replace('','_');
                的CheckBox [I] .CheckedChanged + =新的EventHandler(的CheckBox [I] +_的CheckedChanged);
                的CheckBox [I] .WIDTH = 200;
                如果(我== 0)
                {
                    的CheckBox [I] .Location =新System.Drawing.Point(5,10);
                }
                否则如果(ⅰ== 1)
                {
                    的CheckBox [I] .Location =新System.Drawing.Point(5,40);
                }
                否则如果(ⅰ== 2)
                {
                    的CheckBox [I] .Location =新System.Drawing.Point(5,80);
                }
                this.Controls.Add(的CheckBox [I]);
            }        }


解决方案

 私人无效Form1_Load的(对象发件人,EventArgs的发送)
{
    // ...
    的CheckBox [I] .CheckedChanged + = checkBoxes_CheckedChanged;
    的CheckBox [I] .CheckStateChanged + = checkBoxes_CheckStateChanged;
}无效checkBoxes_CheckedChanged(对象发件人,EventArgs的发送)
{//检查时做的东西改变}无效checkBoxes_CheckStateChanged(对象发件人,EventArgs的发送)
{//做的东西时,检查状态变化}

请注意:这会给相同的事件处理您的所有复选框。
如果你想为不同的文本框做不同的事情,你有不同的命名事件处理程序,并定义事件处理程序。

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

 的for(int i = 0; I< listGroup.Count();我++)
    {
        的CheckBox [I] =新的复选框();
        的CheckBox [I]。文本= listGroup.ElementAt(我).GroupName;
        的CheckBox [I] .Name点=TXT+ listGroup.ElementAt(I).GroupName.Replace('','_');
        的CheckBox [I] .CheckedChanged + =新的EventHandler(的CheckBox [I] +_CheckedChanged);
        的CheckBox [I] .WIDTH = 200;
        //设置区域基于i的索引
        的CheckBox [I] .Location =新System.Drawing.Point(5,10 +(我* 30));
        this.Controls.Add(的CheckBox [I]);
    }

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

        }

解决方案

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

这篇关于添加事件处理程序动态创建的,以窗口形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆