Determine control type

  • Thread starter Thread starter CJack
  • Start date Start date
C

CJack

hi, i have a window form with different controls. i want to loop
through all the controls and write the types and lables of each
control in a file. I dnt know how to determine the type of a control
that wether it is a button, lable or text box.
any body out there to help please.

thanks in advance
 
(e-mail address removed) (CJack) wrote in @posting.google.com:
hi, i have a window form with different controls. i want to loop
through all the controls and write the types and lables of each
control in a file. I dnt know how to determine the type of a control
that wether it is a button, lable or text box.
any body out there to help please.

thanks in advance

foreach(Control c in Controls)
{
Button b = c as Button;
if(n!=null)
{
//Button code
return;
}

TextBox tb = c as TextBox;
if(tb!=null)
{
//TextBox code
return;
}
}
 
Peter Koen said:
(e-mail address removed) (CJack) wrote in @posting.google.com:


foreach(Control c in Controls)
{
Button b = c as Button;
if(n!=null)
{
//Button code
return;
}

TextBox tb = c as TextBox;
if(tb!=null)
{
//TextBox code
return;
}
}

--

Just use object.GetType() and Control.Text to get the system type of each
control and its label.

e.g.
foreach (Control c in Controls)
{
Console.WriteLine("Control Type = {0}, Label = {1}",
c.GetType(),
c.Text);
}

Since all controls have the Text property, there is no need to coerce the
controls to their specific type.

However, if you do need to then the above code certainly does the trick.
Likewise you can use the "is" keyword if you don't actually need to operate
on the control.

if (c is Button)
// Special case for Button controls

-- TB
 
Back
Top