"Peter Koen" <koen-newsreply&snusnu.at> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl...
sh*******@hotmail.com (CJack) wrote in news:2e52a6a3.0310251301.275216a0
@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;
}
}
--
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