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

check whether a textbox is empty

P: n/a
Hi there

What's the best way of checking whether a textbox is empty? Thanks.

Yuelin
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
If you want to ignore spaces, then

textBox1.Text.Trim() == ""

otherwise use

textBox1.Text == ""

Chris
"Yuelin" <li***@lineone.net> wrote in message
news:#O*************@TK2MSFTNGP12.phx.gbl...
Hi there

What's the best way of checking whether a textbox is empty? Thanks.

Yuelin

Nov 15 '05 #2

P: n/a
In article <#O*************@TK2MSFTNGP12.phx.gbl>, li***@lineone.net says...
Hi there

What's the best way of checking whether a textbox is empty? Thanks.

Yuelin


Subclass TextBox and add a IsEmpty Method

class MyTextBox : TextBox
{

public bool IsEmpty()
{
return (TextBox.Text == "")
}

}
--
Thanks
Mark Marsella
Nov 15 '05 #3

P: n/a
Seems a bit excessive...
"Mark Marsella" <ne**@tersus-solutions.com> wrote in message
news:MP************************@news.cis.dfn.de...
Subclass TextBox and add a IsEmpty Method

Nov 15 '05 #4

P: n/a
The more .Net oriented way is to use

TextBox.Text == String.Empty

It doesn't really matter but it goes back to that magic number thing.
There should never be an instance theoretically that you should ever
have to put a string or number in your program.

Nick

Mark Marsella wrote:
In article <#O*************@TK2MSFTNGP12.phx.gbl>, li***@lineone.net says...
Hi there

What's the best way of checking whether a textbox is empty? Thanks.

Yuelin

Subclass TextBox and add a IsEmpty Method

class MyTextBox : TextBox
{

public bool IsEmpty()
{
return (TextBox.Text == "")
}

}


Nov 15 '05 #5

P: n/a
I always do

textBox1.Text.Length == 0

I read somewhere that checking the string's length is a faster operation.
Although the performance gain would probably only be useful in a loop, and
there's little chance you're doing this in a loop...

~Greg

"Chris Capel" <ch***@nowhere.com> wrote in message
news:#c**************@TK2MSFTNGP10.phx.gbl...
If you want to ignore spaces, then

textBox1.Text.Trim() == ""

otherwise use

textBox1.Text == ""

Chris
"Yuelin" <li***@lineone.net> wrote in message
news:#O*************@TK2MSFTNGP12.phx.gbl...
Hi there

What's the best way of checking whether a textbox is empty? Thanks.

Yuelin


Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.