有一个winform ,是个mdicontainer,上有很多个button,请问怎样得到这些button的集合?
------------------------------------------------------------------------------
要考虑其他的容器控件,比如说Panel最简单的情况如下
foreach(Button b in this.Controls)
{}但是如果有Panel还要对Panel做一个循环所以最好做一个递归的方法
如下:
private void FindButton(Control c)
{
if(c is Button)
{
MessageBox.Show("Button!");
}
if (c.Controls != null)
{
foreach(Control x in c.Controls)
{
FindButton(x);
}
}
}在窗体里面调用
this.FindButton(this);
------------------------------------------------------------------------------
要考虑其他的容器控件,比如说Panel最简单的情况如下
foreach(Button b in this.Controls)
{}但是如果有Panel还要对Panel做一个循环所以最好做一个递归的方法
如下:
private void FindButton(Control c)
{
if(c is Button)
{
MessageBox.Show("Button!");
}
if (c.Controls != null)
{
foreach(Control x in c.Controls)
{
FindButton(x);
}
}
}在窗体里面调用
this.FindButton(this);
解决方案 »
- C#打开文件夹问题
- 请教,关于OUT
- 大虾帮忙改一下代码,多谢
- 很难的排序问题
- 急等(webform)datagrid如何使用模版列?
- VS2003 C#.net timer控件的应用问题???
- 苦求关于的无模式对话框问题!!牛人帮忙~~~~
- Remoting的问题!~大家帮忙看看!谢谢了!~
- 未能加载文件或程序集,求助
- c#推送ios消息 错误: APNS NotificationFailureException -> 8 : Invalid token
- 大虾,请,请,多,多指点(那里有C#的教材下载,我以前是asp.net的vb,而现在要学c#,不知道该看那些教材,)
- SOS! 关于SaveDialog奇怪的问题——打开“我的文档”时会没相应
private void FindButton(Control c)
{
if(c is TextBox)
{
....Enalbe = false; }
if (c.Controls != null)
{
foreach(Control x in c.Controls)
{
FindButton(x);
}
}
}
{
b.Enabled =false;
}不好使呀。
看看书再说话,foreach经常用的。