|
Hi everyone, what is the best way for me at runtime to go through objects and figure out all objects that are windows forms or user controls?
I know that all controls inhert the control class, but how do I discover the hierarchy at runtime, do I loop through the fields collection ( with bindingflags as instance and public ) ?
and I do want to limit the collection to user controls and windows forms, so as to weed out a button, or a panel!!.
Thanks everyone.
| | kalamantina Thursday, September 10, 2009 3:44 PM | Hi, I hope it will help you
private void FindControls()
{
foreach (Control ctrl in this.Controls)
{
MessageBox.Show(ctrl.Name.ToString());
}
}
Best Regards,
C.Gnanadurai
-----------------------
Please mark the post as answer if it is helpfull to you - Marked As Answer byLing WangMSFT, ModeratorThursday, September 17, 2009 7:18 AM
-
| | Gnanadurai Friday, September 11, 2009 6:10 PM | At an appropriate point in your code do this:
...
foreach(Form f in Application.OpenForms)
{
System.Diagnostics.Trace.WriteLine(f.Name);
DumpCtrls(f,1);
}
...
private void DumpCtrls(Control ctrl,int level)
{
foreach(Control c in ctrl.Controls)
{
if(c is UserControl)
System.Diagnostics.Trace.WriteLine(string.Format("{0,"+3*level+"}{1}",string.Empty,c.Name));
DumpCtrls(c,level+1);
}
}
- Proposed As Answer byk.m Friday, September 11, 2009 7:25 PM
- Marked As Answer byLing WangMSFT, ModeratorThursday, September 17, 2009 7:18 AM
-
| | k.m Friday, September 11, 2009 7:09 PM | Use the "is" operator: public partial class Form1 : Form { public Form1() { InitializeComponent(); findUserControls(this.Controls); } private void findUserControls(Control.ControlCollection ctls) { foreach (Control ctl in ctls) { if (ctl is UserControl) Console.WriteLine(ctl.Name); findUserControls(ctl.Controls); } } }
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorThursday, September 17, 2009 7:18 AM
-
| | nobugz Tuesday, September 15, 2009 1:45 AM | Hi, I hope it will help you
private void FindControls()
{
foreach (Control ctrl in this.Controls)
{
MessageBox.Show(ctrl.Name.ToString());
}
}
Best Regards,
C.Gnanadurai
-----------------------
Please mark the post as answer if it is helpfull to you - Marked As Answer byLing WangMSFT, ModeratorThursday, September 17, 2009 7:18 AM
-
| | Gnanadurai Friday, September 11, 2009 6:10 PM | At an appropriate point in your code do this:
...
foreach(Form f in Application.OpenForms)
{
System.Diagnostics.Trace.WriteLine(f.Name);
DumpCtrls(f,1);
}
...
private void DumpCtrls(Control ctrl,int level)
{
foreach(Control c in ctrl.Controls)
{
if(c is UserControl)
System.Diagnostics.Trace.WriteLine(string.Format("{0,"+3*level+"}{1}",string.Empty,c.Name));
DumpCtrls(c,level+1);
}
}
- Proposed As Answer byk.m Friday, September 11, 2009 7:25 PM
- Marked As Answer byLing WangMSFT, ModeratorThursday, September 17, 2009 7:18 AM
-
| | k.m Friday, September 11, 2009 7:09 PM | HI K.M,
This only gives me the name, I amlooking for a way to see if this is either a user control or a windows form, as in it inherits usercontrol or windows.form class. I am looking for a good way to do that.
Thanks. | | kalamantina Tuesday, September 15, 2009 1:23 AM | HI C.Gnanadurai ,
This only gives me the name, I amlooking for a way to see if this is either a user control or a windows form, as in it inherits usercontrol or windows.form class. I am looking for a good way to do that.
Thanks. | | kalamantina Tuesday, September 15, 2009 1:23 AM | Use the "is" operator: public partial class Form1 : Form { public Form1() { InitializeComponent(); findUserControls(this.Controls); } private void findUserControls(Control.ControlCollection ctls) { foreach (Control ctl in ctls) { if (ctl is UserControl) Console.WriteLine(ctl.Name); findUserControls(ctl.Controls); } } }
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorThursday, September 17, 2009 7:18 AM
-
| | nobugz Tuesday, September 15, 2009 1:45 AM |
|