473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

determin if a object is a textbox

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.
--
mo*******@noemail.noemail
Mar 16 '07 #1
10 5923
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
"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
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.
--
mo*******@noemail.noemail

Mar 17 '07 #2
void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}

"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
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.
--
mo*******@noemail.noemail

Mar 17 '07 #3
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)" <x_****@timney.eclipse.co.ukwrote in message
news:Pp*********************@eclipse.net.uk...
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
"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
>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.
--
mo*******@noemail.noemail


Mar 17 '07 #4
Indeed they do - good suggestion - not considered that in my reply!

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"Scott M." <s-***@nospam.nospamwrote in message
news:en**************@TK2MSFTNGP06.phx.gbl...
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)" <x_****@timney.eclipse.co.ukwrote in message
news:Pp*********************@eclipse.net.uk...
>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
"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
>>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.
--
mo*******@noemail.noemail



Mar 17 '07 #5
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." <s-***@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}

"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
>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.
--
mo*******@noemail.noemail


Mar 17 '07 #6
I get a compile error on getType. sender (object) doesnt contain a
definitin for getttype. Any other ideas?

"Scott M." <s-***@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}

"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
>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.
--
mo*******@noemail.noemail


Mar 17 '07 #7
This is it:

sender.GetType().Name

"moondaddy" <mo*******@noemail.noemailwrote in message
news:OO**************@TK2MSFTNGP02.phx.gbl...
>I get a compile error on getType. sender (object) doesnt contain a
definitin for getttype. Any other ideas?

"Scott M." <s-***@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}

"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
>>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.
--
mo*******@noemail.noemail



Mar 17 '07 #8
VJ
GetType()

C-Sharp is case senstive langugae

VJ

"moondaddy" <mo*******@noemail.noemailwrote in message
news:OO**************@TK2MSFTNGP02.phx.gbl...
>I get a compile error on getType. sender (object) doesnt contain a
definitin for getttype. Any other ideas?

"Scott M." <s-***@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>void Test(object sender)
{
if(sender.getType.name == "Texbox")
{
// do something.
}
}

"moondaddy" <mo*******@noemail.noemailwrote in message
news:e0****************@TK2MSFTNGP06.phx.gbl...
>>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.
--
mo*******@noemail.noemail



Mar 17 '07 #9
Scott M. <s-***@nospam.nospamwrote:
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)
{
...
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 17 '07 #10
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 19 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Laurence Nixon | last post by:
Hello all. I am seeking ideas on how to determin a specific value with varied parameters. This will be for students who have taken modules in a course for pre-certification. So it will be like...
0
by: Magnabyte | last post by:
Hello all. I am seeking ideas on how to determin a specific value with varied parameters. This will be for students who have taken modules in a course for pre-certification. So it will be like...
4
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than...
0
by: feng | last post by:
I want to use onunload to capture user's action when he clicks on the "x" button, the close window button on the upper-right corner of the brower window, to close the browser. Here is how I...
1
by: Tancev Sasa | last post by:
I am interesting how to determin is the selected printer a dot-matrix printer or is it a laser printer What property is relevant to detrmin what type is selected printer
4
by: Alex | last post by:
Dear colleagues How can I determin if "return button was pressed" in a textbox? I am trying to di this with this methode: Private Sub txbInput_KeyDown(ByVal sender As Object, ByVal e As...
2
by: jakobsg | last post by:
Hi. How can I determin the hwaddr or ipaddress of the networkinterface that is used in an outgoing connection? I need a kind of traceroute in python or lowlevel socket function that can help me...
3
by: aProgrammingStudent | last post by:
Here's the deal. I am creating a calculator program using visual studios. I have a much of buttons on my form and I want to be able to determin which button is clicked by the user. Reason: I...
7
by: Nick | last post by:
Hi there, Is it possible to tell how a WebMethod was invoked? For example I would like to determin if it was invoked via SOAP or HTTP Post. Other than creating 2 methods I am no sure if I can...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.