PC Review


Reply
Thread Tools Rate Thread

Cycle through components on a form at run time

 
 
Andrew
Guest
Posts: n/a
 
      20th Dec 2006
I have a need to cycle through the compoents on the form in my program at
run time to check for the existence of certain components.

I do *not* mean the Controls via the form.Controls array, I want the non
visual components.

I know the components are not technically part of the form but there must be
a way to access the list of them.

Does anyone know how?

Thanks

--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.


 
Reply With Quote
 
 
 
 
Oliver Sturm
Guest
Posts: n/a
 
      20th Dec 2006
Hello Andrew,

>I have a need to cycle through the compoents on the form in my program at
>run time to check for the existence of certain components.


The most reliable way to do this would involve using Reflection. For
instance, this snippet outputs the name of each Component that is stored
in a private field in the Form derived class (which is true at least for
all the components the VS designer handles):

Type formType = this.GetType( );
FieldInfo[] fields = formType.GetFields( BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (FieldInfo field in fields) {
if (typeof(Component).IsAssignableFrom(field.FieldType))
Debug.WriteLine(field.Name);
}

For your purpose I guess you'd have to modify this a bit - for example,
use field.GetValue() to retrieve a reference to the actual component
instance or something like that.


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
Andrew
Guest
Posts: n/a
 
      20th Dec 2006
Thanks Oliver,

I was begining to think this would be the only way. Its a pity they didn't
add a components array like in Delphi.

--
Andrew Cutforth - AJC Software - www.ajcsoft.com
The best folder synchronize and directory compare tool available.
AJC Active Backup instantly archives every file you edit giving you
unlimited undo and automatic revision control. Never lose your data again.



 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      20th Dec 2006
Hello Andrew,

>I was begining to think this would be the only way. Its a pity they
>didn't add a components array like in Delphi.


Well, yes... but then, with Reflection being as good as it is in .NET, it
not necessary to anticipate every such requirement at an early stage.
Those five or so lines of code in a GetComponents(Form form) method and
you're done.


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: cycle time Barb Reinhardt Microsoft Excel Charting 0 21st Aug 2008 03:24 PM
Time Cycle Antonio Microsoft Excel Misc 7 3rd Dec 2007 11:01 PM
Accesing form's components in design time =?Utf-8?B?c2Ftbm9zcGFtQG5vc3BhbS5ub3NwYW0=?= Microsoft Dot NET Framework Forms 7 5th Apr 2006 09:06 AM
Organizing Windows Form UI Components at Design Time curtis.johnstone@gmail.com Microsoft Dot NET Framework Forms 4 24th Oct 2005 11:22 PM
Dynamic Form Components at Design Time =?Utf-8?B?Q2hhcmxpZQ==?= Microsoft VB .NET 4 23rd Dec 2004 04:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:18 AM.