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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing a component quickly

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I have a series of labels / textboxes within a form.
How do I quickly access one of these according to the name of the component ?
Currently we use syntax similar to the following :

for (int i=0; i<panel1.Controls.Count;i++)
{
if (panel1.Controls.Name.ToString() == &quot;TextBox1&quot;)
{
// We now know we are dealing with TextBox1 component
}
}

Is there a more direct way of doing this ?
Would the Tag's against the components be a quicker way of finding things ?

Within Borland Delphi I recall the 'FindComponent' function which allowed a quick handle to the component in question (by name I think) - is there an equivalent in C# ?

I'm not asking because the current method is slow - it just feels there should be a more direct route.

Any suggestions would be appreciated.
Thanks in advance
Steve
 
The only slow part about your code is that your loop will be testing the name of all the components that aren't a &quot;Textbox1&quot; (checkboxes, tabs, etc.). If you know the control names ahead of time you could place them in an array. Or perhaps build the array at form load-time, so you only have to do it once.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top