获取动态创建的复选框 [英] getting the checkbox which was created dynamically
问题描述
Panel pan = (Panel) this.tabPage1.Controls.Find("a" + i.ToString(),true);
CheckBox ck = (CheckBox)this.tabPage1.Controls.Find("check" + i.ToString(), true);
在上面的代码中,我正在C#.net(Windows应用程序)中动态创建多个面板,并且每个面板都动态添加了复选框.
在这里,我的要求是访问特定的选中复选框并将值写入相应的数据库
在上面的代码中,ai表示(a1,a2,a3 .........)动态创建的面板
checki表示(check1,check2,.........)动态创建的复选框
它有什么错误
以及我应该如何访问它
而且我得到的错误是
1).无法将type.system.windows.forms.controls []转换为system.windows.forms.panel
2).无法将type.system.windows.forms.controls []转换为system.windows.forms.checkbox
in above code that i am creating number of panels dynamically in C#.net(windows application)and in that each panel that am addng the checkboxs dynamically
here my requirement is that accessing the particular checked checkbox and writintg the value to the corresponding database
in above code that ai means (a1,a2,a3.........)panels created dynamically
checki means (check1,check2,.........)checkboxs created dynamically
what is the error in it
and how should i access it
more over i am getting the error as
1).cannot converttype system.windows.forms.controls[] to system.windows.forms.panel
2).cannot converttype system.windows.forms.controls[] to system.windows.forms.checkbox
推荐答案
尝试类似的方法:
Try something like:
Panel pan = (Panel)this.tabPage1.Controls.Find("a" + i.ToString(), true)[0];
CheckBox ck = (CheckBox)this.tabPage1.Controls.Find("check" + i.ToString(), true)[0];
当然,您还应该检查要查找的控件是否存在并且确实是复选框或面板:
Of course you should also check if the control you''re looking for exists at all and is indeed a checkbox or panel:
Control[] panels = this.tabPage1.Controls.Find("a" + i.ToString(), true);
Panel pan = null;
CheckBox ck = null;
if (panels.Length > 0 && panels[0] is Panel)
{
pan = (Panel)panels[0];
}
else
{
//The panel does not exist - do whatever you think is appropriate
}
Control[] checkBoxes = this.tabPage1.Controls.Find("check" + i.ToString(), true);
if (checkBoxes.Length > 0 && checkBoxes[0] is CheckBox)
{
ck = (CheckBox)checkBoxes[0];
}
else
//Checkbox doesn't exist
这篇关于获取动态创建的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!