比如我窗体上有两个文本框,还有一个PANEL,PANEL中有一个按钮,我现在遍历界面上所有的控件,如果不进入PANEL的话那就是两个文件框和一个PANEL,我现在想得到两个文本框,一个PANEL,一个按钮,难道必须进对PANEL进行遍历才可以?
解决方案 »
- 动态生成的复选框如何实现多个级联选择???
- 如何保证实时数据的完整
- 如何用C#在WINFORM中,制作自定义的流程图或者是工作流? 有没有一些第三方的软件或插件,有源码的最好。
- 求一WPF TextBox Style
- 请大家说说反射的好处
- C#与485串行通信问题
- winform如何查看CPU的序列號?
- 问问各位,在没有.net环境下,怎么运行debug下的c#应用程序?
- 如何Serialize一个MarshalByRefObject?
- 请问在C#.Net 下 怎么处理C/S模式下的 在Internet上的DATASET的传输啊 数据量可能比较大
- .net 本机不同应用程序间通信
- 请教:关于使用 XSD 验证XML 的初学者问题?
foreach(FieldInfo fi in typeof(Form1).GetFields(BindingFlags.Private | BindingFlags.Instance | BindingFlags.DeclaredOnly)
{
//判断fi的FieldType为控件类型
}
foreach (Control con in this.Controls)
{
if (con is TextBox)
{
MessageBox.Show(con.Text);
}
}
想全部遍历出来,只要递归一下就可以了。
譬如:将得到的某个控件control.Controls.count判断,如果为0就添加,如果为1再继续遍历。
不麻烦
foreach (Control con in this.Controls)
{
if (con is Panel)
{
Panel panel = con as Panel;
foreach (Control cont in panel.Controls)
{
if (cont is TextBox)
{
MessageBox.Show(cont.Text);
}
}
}
}
foreach (Control con in this.Controls)
{
if (con is Panel)
{
Panel panel = con as Panel;
foreach (Control cont in panel.Controls)
{
if (cont is Button)
{
MessageBox.Show("按钮");
}
}
}
else if (con is TextBox)
{
MessageBox.Show("文本框");
}
}
foreach (System.Windows.Forms.Control contr in this.Controls)
{
string name = contr.Name;
foreach (System.Windows.Forms.Control contr2 in contr.Controls)
{
string name2 = contr2.Name;
}
}
http://blog.csdn.net/dunao/archive/2010/10/28/5972927.aspx
先问问控件 HasChildren 之后再遍历.
if(ctl.Controls.Count>0)
递归
else
return;
挺容易实现的