用代码动态生成的控件我怎么处理它的事件?(高分)

qozm 2004-04-29 05:50:53

Button button1 = new Button();
button1.Click += new System.EventHandler(button1_Click);

然后用一个循环生成了几个(具体数量不定)checkBox控件,
CheckBox cb = new CheckBox();

再生成一个ListBox控件
ListBox cb = new ListBox();

我选择checkBox中的几个打勾,然后点击按钮button1,
把选中的checkBox.Text加listBox中去,
我该怎么写代码才行?
...全文
68 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
smartcreater 2004-04-29
  • 打赏
  • 举报
回复
//define a class than bind a adding Text to ListBox functon to the Button event
public class myCheckBox :CheckBox
{
private Button bt;
private ListBox lb;

//constructor
public myCheckBox(Button bt1,ListBox lb1)
{
this.bt = bt1;
this.lb = lb1;
this.bt.Click += new System.EventHandler(this.AddTextToListBox);
}

private void AddTextToListBox()
{
if(this.Checked)
{this.lb.Items.Add(this.Text);}
}
}

//note : you must create ListBox ,Button object first
//,then create CheckBox(s) object



Button button1 = new Button();
button1.Click += new System.EventHandler(button1_Click);
ListBox lb = new ListBox();

//note : you shold crete myCheckBox type object ,not CheckBox
for(int i = 0 ;i<100;i++
{
myCheckBox cb = new myCheckBox(button1,lb);
}


///----------end (not test the code , i think you can debug it)
cppTrier 2004-04-29
  • 打赏
  • 举报
回复
你生成的时候就会一个CheckBox的数组:

pivate CheckBox [] cbArr;

生成:
cbArr = new CheckBox[n];

然后按钮事件:

foreach(CheckBox cb in cbArr)
{
if(cb.Checked)
{
listbox.items.add(c.Text);
}
}
EdwarddotNet 2004-04-29
  • 打赏
  • 举报
回复
我的代码,自己参照改一下吧
for(int j = 0; j < tc.TabPages[i].Controls.Count ; j ++)
{
GroupBox gb = new GroupBox();
if(object.ReferenceEquals(tc.TabPages[i].Controls[j].GetType(),gb.GetType()))
{
for(int h = 0; h < tc.TabPages[i].Controls[j].Controls.Count ; h ++)
{
RadioButton rb = new RadioButton();
if(object.ReferenceEquals(tc.TabPages[i].Controls[j].Controls[h].GetType(),rb.GetType()))
{
rb = (RadioButton)tc.TabPages[i].Controls[j].Controls[h];
rb.Checked = false;
}
}
}
}
czy412 2004-04-29
  • 打赏
  • 举报
回复
这是我刚写的,没有测试,所以可能有点问题,但是应该问题不大的.
czy412 2004-04-29
  • 打赏
  • 举报
回复
CheckBox cb;
ArrayList ary = new ArrayList();
for (int i = 0 ; i < 5 ; i ++)
{
cb = new CheckBox();
ary.Add(cb);
}

private void button1_Click(object sender , System.EventArgs e)
{
//你首先得遍历找出你有哪几个checkBox控件,然后将其checkBox.Text加listBox中去
//我不知道你怎么建立这个数组的??
for(int i = 0 ; i < ary.Count; i++)
{
CheckBox c = (CheckBox) ary[i];
if(c.Checked)
{
listbox.items.add(c.Text);
}
}
}

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧