HOWTO · Csharp
How to Check if TextBox Is Empty in C#
Check a Windows Forms TextBox with String.IsNullOrEmpty() or TextBox.Text.Length, and learn when whitespace-aware validation is needed.
On this page
To check a Windows Forms text box, read its Text property when the user clicks a button. Use String.IsNullOrEmpty() when only a null or zero-character value should match. Choose String.IsNullOrWhiteSpace() when spaces and other whitespace should match too. Both approaches work with the text returned by TextBox.Text; the difference is how they classify whitespace-only input.
Check if a TextBox Is Empty With the String.IsNullOrEmpty() Function in C#
String.IsNullOrEmpty() returns true when its argument is null or "", and false for text made only of spaces. For a Windows Forms button handler, pass textBox1.Text as the argument. If the condition matches, the handler can set label1.Text to "TEXT BOX IS EMPTY". That conditional updates the label only when it matches; it does not specify what the label should show for other input.
This standalone C# console program uses a sample value and writes a short status to standard output. It keeps the decision in one place, so you can adapt the same comparison inside a Windows Forms button handler. The example below uses a simple conditional branch and reports the selected message; the controls and event wiring are not part of this console example.
using System;
class Program
{
static void Main()
{
string text = "";
if (String.IsNullOrEmpty(text))
{
Console.WriteLine("No characters");
}
else
{
Console.WriteLine("Text is present");
}
}
}
No characters
When spaces or other whitespace should count as missing input, call String.IsNullOrWhiteSpace(textBox1.Text) instead. This method returns true for null, an empty string, or text containing only whitespace. Its exact name is String.IsNullOrWhiteSpace(); String.IsNullOrWhitespace() and String.IsEmptyOrNot() are not valid .NET methods. The console sample isolates the string decision from the user interface. The button-handler expression is the Windows Forms adaptation for a form that already has controls named textBox1 and label1; it requires that form and is not a standalone console program.
Choose the whitespace-aware method when a field containing only spaces should be treated like missing input, such as before showing a required-field message. Use String.IsNullOrEmpty() when spaces are meaningful data and should remain distinct from a zero-character value. Neither check trims or changes the text; each returns a Boolean result that your handler can use to decide what to do.
Check if a TextBox Is Empty With the TextBox.Text.Length Property in C#
TextBox.Text.Length gives the number of characters in the control’s text. In a Windows Forms handler, textBox1.Text.Length == 0 matches a zero-character string. A value containing spaces has a nonzero length, so this condition does not identify whitespace-only input. Use this property when the distinction is specifically between no characters and one or more characters; use String.IsNullOrWhiteSpace() if whitespace should also match.
The length comparison is easy to read when the value is already available from a non-null TextBox.Text property. It reports only the character count condition: it does not trim spaces, update a label, or perform other field validation. Put the comparison inside the event handler or validation logic that needs the result, and choose the string method instead when the whitespace distinction matters.