Reputation: 309
I'm trying to make an if statement to check whether a TextBox
has text it.
So something like this:
if (textbox1 has text)
{
//Then do this..
}
How would I write "textbox1 has text"?
Upvotes: 0
Views: 46918
Reputation: 21
That's all very well, but I think there's something else. Since the text field (textbox) can contain only spaces (for example) and how it should be detected in that case, I know that using Regex but I'm not very expert to say. Using the HasText option is nice, but it only checks if something exists and doesn't specify if it's not spaces. Although a combination with IsNullOrWhiteSpace should also be useful. I'll try it. Thank you!
Upvotes: 0
Reputation: 841
I have seen all the other answers on the page, suggesting solutions like
if (tb.Text == "")
{
// Is empty
} else {
// Is not empty
}
However this could easily break, and return that a text box is not empty, when it actually is. For example, a few spaces in the text box (and nothing else) would pass as not empty, when in fact, while not technically empty, you would probably want to report it as empty. A solution to this is the C# string.Trim()
method. This removed all whitespace. Now changing my solution to this:
if (tb.Text.Trim() == "")
{
// Is empty
} else {
// Is not empty
}
... it would no longer report a text box full with spaces as not empty.
Upvotes: 1
Reputation: 101
the simpliest logic is this:
if (textBox1.Text != "")
MessageBox.Show("The textbox is not empty!");
else
MessageBox.Show("The textbox is empty!");
Upvotes: 1
Reputation: 2372
Simple. Check out this MSDN page: string.IsNullOrEmpty
if (!string.IsNullOrEmpty(textBox1.Text))
{
}
Alternatively, you could use a property. This is especially helpful when you need to check multiple times to see if the textbox has text:
// a public property is not necessary for this
bool HasText
{
get
{
return !string.IsNullOrEmpty(textBox1.Text);
}
}
...
if (HasText)
{
}
Another way to go about doing this is by using an extension method:
public static class TextBoxUtility
{
public static bool HasText(this TextBox textBox)
{
return !string.IsNullOrEmpty(textBox.Text);
}
}
...
if(textBox1.HasText())
{
}
I prefer the latter instead of a property because it works across all textboxes.
Upvotes: 1
Reputation: 3292
Checking the Length
if (textBox1.Text.Length > 0)
{
// perform task
}
Using the Null
if (!string.IsNullOrEmpty(textBox1.Text))
{
// perform a task
}
Using Trim
would be nice for checking if a user is only putting spaces.
if (textBox1.Text.Trim().Length > 0)
{
// perform a task
}
Try any one of these and it should work. There are way more ways but since this question is a bit broad it's up to you.
Upvotes: 1
Reputation: 26
if(textBox1.Text!=null)
{
///code
}
or
if(textBox1.Text!=string.Empty)
{
//code
}
Upvotes: 0
Reputation: 6492
You can get the text stored in the TextBox
using Text
property of the TextBox
and then check whether it is null or empty like this :-
string text = textBox1.Text ;
if(String.IsNullOrEmpty(text))
{
// Do something
}
Upvotes: 3
Reputation: 31339
Check the length of the text box text.
if (textbox1.Text.Length > 0)
{
//do the process here
}
Upvotes: 2
Reputation: 5305
if (textbox1.Text.Length > 0)
{
...
}
or
if (!string.IsNullOrEmpty(textbox1.Text))
{
...
}
Upvotes: 26