Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Form Panels Programmatically

Status
Not open for further replies.

skip2004

Programmer
Nov 9, 2004
37
GB
Hi,

Hopefully this is an easy question and there is a simple solution!

Is it possible to programmatically accessing the panels in a windows form?

My form has 5 panels, so if I wanted to loop through the controls in all 5 panels I could, for example, do:

For Each objControl In panel1.Controls
objControl.DoSomething....
Next

For Each objControl In panel2.Controls
objControl.DoSomething....
Next

For Each objControl In panel3.Controls
objControl.DoSomething....
Next

etc, etc for all 5.

Is there anyway I can programmatically set the panel I wish to loop through to save the need to repeat the code for each of the 5 panels as detailed above?

I'm certain it must be possible, and hopefully I'm just missing something obvious!

Thanks in advance,
skip
 
Thanks.

I was hoping there would be a more direct approach at getting to the panel rather than having to loop through all the controls.
 
Thanks Christiaan, that is an interesting article.

Code works fine on controls added at design time, but does not work on dynamic controls.

The form I have is built up of (mainly) dynamic controls, and it's those I'd like to retrieve by name.

Do you know of a way of retrieving dynamic controls using the System.Reflection.FieldInfo method?

Many thanks,
skip
 
Are you setting the .Name property of your dynamically created controls?
 
Yes, the .Name property is being set.

I think the limitation of the Reflection method is that it doesn't locate dynamically created controls - hoping that isn't the case though.

Thanks,
skip
 
Nope it won't work on dynamically created objects, and why should it? You created them so you know what you created, right.


Christiaan Baes
Belgium

My Blog
 
Ah, I missed the part where you guys were talking about reflection. I don't see looping through the controls collection as such a big deal. If you've got hundreds of controls on your form, you're going to be slow for other reasons than the loop.
 
The controls are dynically built based on some sql table rows - so I don't create them as such.

I was wanting the best way to get to specific control(s) without having to loop through the controls collection - I thought he relection method may be quicker/more effecient.

Thanks,
skip
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top