A Alberto Oct 22, 2004 #1 How can I find out the type of a Control? In my case, it could be a TextBox or a CheckBox. Thank you.
How can I find out the type of a Control? In my case, it could be a TextBox or a CheckBox. Thank you.
G Guest Oct 22, 2004 #2 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 }
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 }
R Richard Blewett [DevelopMentor] Oct 22, 2004 #3 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.
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.
A Anders Borum Oct 22, 2004 #4 What's wrong with: if (thisControl is TextBox) { // TextBox control .. } else if (thisControl is DropDownBox) { // DropDownBox control .. } else { // Another control .. }
What's wrong with: if (thisControl is TextBox) { // TextBox control .. } else if (thisControl is DropDownBox) { // DropDownBox control .. } else { // Another control .. }
G Guest Oct 23, 2004 #5 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
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