check whether a textbox is empty

  • Thread starter Thread starter Yuelin
  • Start date Start date
If you want to ignore spaces, then

textBox1.Text.Trim() == ""

otherwise use

textBox1.Text == ""

Chris
 
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 == "")
}

}
 
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
 
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
 
Back
Top