By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,954 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,954 IT Pros & Developers. It's quick & easy.

Determine control type

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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;
}
}

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at
Nov 15 '05 #2

P: n/a
TB
"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
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.