473,395 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

List all controls inside of each form

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
4 13329

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roger | last post by:
Hi In a Windows forms application I have 2 forms A and B; Form B inherits from form A. Form A is never displayed and its only purpose is to be inherited from and therefore contains mostly...
6
by: TomislaW | last post by:
How to find all user controls (ascx) loaded on a Page?
4
by: Milsnips | last post by:
hi there, i have a strange problem. I want to programatically loop through each control on a page, but am having issues. my test example: 1. header.ascx - the ascx control finds all the...
4
by: Ali | last post by:
I used to clear my page's control in Visual Studio 2003 using code like this: Dim c As Control For Each c In Page.Controls(1).Controls If TypeOf c Is TextBox Then CType(c, TextBox).Text = Nothing...
6
by: Ryo | last post by:
Hello ! I have an .aspx page that use a master page. I want to do a for each that list all controls on this page. I have try something like: foreach (System.Web.UI.Control wct in this.Controls)...
2
by: MLH | last post by:
I would like to populate a table with the following information: tblPropertySettings - the GotFocus property setting string - the LostFocus property setting string I'd like to document...
15
by: Arpan | last post by:
Consider the following code which retrieves data from a SQL Server 2005 DB table & displays it in a DataGrid: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
5
by: nktt3514 | last post by:
I'm using a assembly to load my apllication, and now I need to list all forms in my apllication and list all controls. I have read the topic on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.