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

Iterate over Collection Controls

Status
Not open for further replies.

4ks

Programmer
Jan 27, 2007
5
AU
I am trying to iterate over all controls in a panel components collection to retrieve value from nested TextBox components. the code below produces an error.
TextBox' is a 'type' but is used like a 'variable'. What is the best way to do this?

public void ListControlCollection()
{
foreach (Control ctrl in assesmentPanel.Controls)
{
if (ctrl.GetType().Equals(TextBox))
{
question_txt.Text = ctrl.Text;
}
}
}
 
Or,

Code:
if (ctrl is TextBox)
    question_txt.Text = ((TextBox)ctrl).Text;

 
if (ctrl.GetType().Equals(typeof(TextBox)))

You'll want to cache the [tt]typeof(TextBox)[/tt] in a local variable outside the loop. It turns out to be a very expensive operation -- we sped up our app tremendously by removing unneeded typeof() and GetType() calls.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top