By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,565 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

List all controls inside of each form

P: n/a
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to
list all forms in my apllication and list all controls (ex: txtname;btnname)
inside of each form.
Oct 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Toze wrote:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to
list all forms in my apllication and list all controls (ex: txtname;btnname)
inside of each form.
Technically, controls don't exist inside your form until after the
InitializeComponent() method runs; before that, it's just a bunch of
code that _will_ add controls when run.

If you mean, "all instantiated forms" as in "all the ones showing on
the screen" then you just have to query each form's Controls
collection. If you mean all of the Form classes defined / used by your
application, then you have to instantiate each one and then look at its
Controls collection.

You can get the list of classes defined by an assembly using
Reflection, and then from amongst those find the ones that are derived
(directly or indirectly) from System.Windows.Forms.Form. Then you have
to instantiate each one using "new" and look at its Controls collection.

Oct 5 '06 #2

P: n/a
Give me a sample to do that,
list of classes defined by an assembly using
Reflection,to instantiate each one using "new" and look at its Controls
collection

"Bruce Wood" wrote:
>
Toze wrote:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to
list all forms in my apllication and list all controls (ex: txtname;btnname)
inside of each form.

Technically, controls don't exist inside your form until after the
InitializeComponent() method runs; before that, it's just a bunch of
code that _will_ add controls when run.

If you mean, "all instantiated forms" as in "all the ones showing on
the screen" then you just have to query each form's Controls
collection. If you mean all of the Form classes defined / used by your
application, then you have to instantiate each one and then look at its
Controls collection.

You can get the list of classes defined by an assembly using
Reflection, and then from amongst those find the ones that are derived
(directly or indirectly) from System.Windows.Forms.Form. Then you have
to instantiate each one using "new" and look at its Controls collection.

Oct 6 '06 #3

P: n/a
Well, I don't have time to write and test the program for you, but I
can point you toward the right classes and methods to use.

If you look at the Assembly class, you'll see that you can use the
GetTypes() method to get all of the types defined in the assembly. If
you need to look in assemblies that are called by your main program,
you can use GetExecutingAssembly to get the assembly for the executing
program, then GetReferencedAssemblies to follow the tree of referenced
assemblies from there.

For any particular assembly, as I mentioned you can use GetTypes() to
get the types defined in the assembly. For each type, you can do this:

foreach (assemblyType in assembly.GetTypes())
{
if (assemblyType.IsSubclassOf(typeof(System.Windows.F orms.Form)))
{
// You may need to use assemblyType.FullName here... I don't
know for sure.
System.Windows.Forms.Form f =
(System.Windows.Forms.Form)assembly.CreateInstance (assemblyType.Name,
false, System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic, null, null, null, null);
foreach (Control c in f.Controls)
{
... do something with control c of form f ...
}
f.Dispose();
}
}

Toze wrote:
Give me a sample to do that,
list of classes defined by an assembly using
Reflection,to instantiate each one using "new" and look at its Controls
collection

"Bruce Wood" wrote:

Toze wrote:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to
list all forms in my apllication and list all controls (ex: txtname;btnname)
inside of each form.
Technically, controls don't exist inside your form until after the
InitializeComponent() method runs; before that, it's just a bunch of
code that _will_ add controls when run.

If you mean, "all instantiated forms" as in "all the ones showing on
the screen" then you just have to query each form's Controls
collection. If you mean all of the Form classes defined / used by your
application, then you have to instantiate each one and then look at its
Controls collection.

You can get the list of classes defined by an assembly using
Reflection, and then from amongst those find the ones that are derived
(directly or indirectly) from System.Windows.Forms.Form. Then you have
to instantiate each one using "new" and look at its Controls collection.
Oct 6 '06 #4

P: n/a
Note also you would have to iterate (recurse) through each Control
instaces Controls collection (and so on), to handle panels,
tab-controls, etc, with inner controls; and note that many forms
(especially non-designer-created) fill their controls in a lazy
fashion, so they may not show yet... for instance a tab-control might
not actually create the inner controls until you switch tab.

Also - if you recurse the assembly dependency tree, watch out for MS's
cyclic-reference at the bottom of the pile ;-p

Marc

Oct 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.