日期:2014-05-20  浏览次数:20777 次

1.如何枚举一个窗体内的所有控件?又如何枚举一个控件的所有属性名称?
请教

------解决方案--------------------
1.如何枚举一个窗体内的所有控件(winform)
====================================
private void button1_Click(object sender, EventArgs e)
{
foreach (Control c in Controls)
{
MessageBox.Show(c.Name);
}
}
------解决方案--------------------
//枚举一个控件的所有属性名称
private void button1_Click(object sender, EventArgs e)
{
Type t = Type.GetType( "System.Windows.Forms.Label, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ");
System.Reflection.PropertyInfo[] p = t.GetProperties();
foreach (System.Reflection.PropertyInfo temp in p)
{
label1.Text += temp.ToString() + "\n ";
}
}
------解决方案--------------------
如何枚举一个窗体内的所有控件(winform)
==================
如果你还需要列出控件内的控件,那么就用到递归,否则amandag的就够了。

递归
List <string> list = new list();
private void getControls(Controls)
{
foreach (Control c in Controls)
{
list.Add(c.Name);
getControls(c);
}
}


调用:
getControls(this);
执行完上面的以后就取list 里面的值。