determin if a object is a textbox

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

in a wpf c# windows app, how do I determin if a control is a texbox?

void Test(object sender)
{
if(sender==Texbox)
{
// do something.
}
}

Thanks.
 
Surely its more important that the textbox has a value, why dont you just
check that instead? The easiest way would be to name the objects
accordingly and check the object name at submission of some form, but you
can probably check the TypeOf control using code along these lines (not test
mind you!!).

Dim myControl As Control
Dim textBox As TextBox
For Each myControl In Controls
If TypeOf myControl Is TextBox Then
textBox = CType(myControl, TextBox)
textBox.Text = "Hello"
End If
Next



Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
 
Textboxes have properties and methods that other controls may not have and
if the OP wants to call a property that is unique to textboxes, he/she would
have to know if the sender is, in fact, a textbox before casting it to one.


John Timney (MVP) said:
Surely its more important that the textbox has a value, why dont you just
check that instead? The easiest way would be to name the objects
accordingly and check the object name at submission of some form, but you
can probably check the TypeOf control using code along these lines (not
test mind you!!).

Dim myControl As Control
Dim textBox As TextBox
For Each myControl In Controls
If TypeOf myControl Is TextBox Then
textBox = CType(myControl, TextBox)
textBox.Text = "Hello"
End If
Next



Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
 
Great thanks guys! This is what I need and yes Scott, I do need to know
what type of control it is first and at this point I'm not concerned with
the value in the control, just it's type.

Scott M. said:
void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}
 
I get a compile error on getType. sender (object) doesnt contain a
definitin for getttype. Any other ideas?

Scott M. said:
void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}
 
GetType()

C-Sharp is case senstive langugae

VJ

moondaddy said:
I get a compile error on getType. sender (object) doesnt contain a
definitin for getttype. Any other ideas?
 
This is it:

sender.GetType().Name

moondaddy said:
I get a compile error on getType. sender (object) doesnt contain a
definitin for getttype. Any other ideas?
 
Scott M. said:
void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}

That's a really bad way of checking it - not only does it rely on
comparing with a literal string, which can easily have a typo in (as
indeed your sample does), but it's also slower than using the operators
which C# already has - "is" and "as".

You can use:

if (sender is TextBox)
{
...
}

or:

TextBox textBox = sender as TextBox;
if (textBox != null)
{
...
}
 
Hi moondaddy,

Yes, GetType().Name property can be used to do the string comparison to
"TextBox". However, a more straight way is using "is" keyword in C#. Such
as:
if (sender is TextBox)
{
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top