所有的Control都在属性Controls里;
所有的Component都在字段components里;
所以这样就可以了:
ArrayList list = new ArrayList();
getControls(this, list);
for (int i = 0; i < components.Components.Count; i++) {
list.Add(components.Components[i]);
}
static void getControls(Control container, ArrayList list) {
for(int i = 0; i < container.Controls.Count; i++) {
list.Add(container.Controls[i]);
}
for(int i = 0; i < container.Controls.Count; i++) {
Control c = container.Controls[i];
if (c.Controls.Count > 0) {
getControls(c, list);
}
}
}
这样就可以得到Form所有定义的Component了。
是不是得到继承Control的所有控件:
Type t = typeof(System.Windows.Forms.Control);
Type[] types = t.Assembly.GetTypes();
for (int i = 0; i < types.Length; i++) {
if (types[i].IsSubclassOf(t)) {
Console.WriteLine(types[i]);
}
}