Find out type of a control

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

How can I find out the type of a Control? In my case, it could be a TextBox
or a CheckBox.

Thank you.
 
You can find out the type of any object by calling the GetType() method of
it...

i.e...

private void frmMain_MyEvent(object sender, System.EventArgs e)
{
System.Diagnostics.Trace.WriteLine(sender.GetType().ToString());

if(sender.GetType() == typeof(System.Windows.Forms.TextBox))
{
....
}

-James
}
 
if( myCtrl.GetType() == typeof(Textbox) )
{
}
else if (myCtrl.GetType() == typeof(Textbox))
{
}
else
{
// arrgghh wrpong control type
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

How can I find out the type of a Control? In my case, it could be a TextBox
or a CheckBox.

Thank you.
 
What's wrong with:

if (thisControl is TextBox)
{
// TextBox control ..
}
else if (thisControl is DropDownBox)
{
// DropDownBox control ..
}
else
{
// Another control ..
}
 
Hi,

Alternatively, you could use the is keyword.

if( <controlname> is Form)
{
}
etc.

is will also ensure that exceptions are not raised if your object is null;
it will evaluate only if the object is not null.

HTH,
Rakesh Rajan
 
Back
Top