Как проверить textbox на пустоту c
You should use String.IsNullOrEmpty() to make sure it is neither empty nor null (somehow):
For practical purposes you might also consider using String.IsNullOrWhitespace() since a TextBox expecting whitespace as input probably negates any purpose, except in case of, say, letting the user pick a custom separator for stuff.
are your best options.
![]()
If one is in XAML, one can check whether there is text in a TextBox by using IsEmpty off of Text property.
Turns out that it bubbles down to CollectionView.IsEmpty (not on the string property) to provide the answer. This example of a textbox watermark, where two textboxes are displayed (on the editing one and one with the watermark text). Where the style on the second Textbox (watermark one) will bind to the Text on the main textbox and turn on/off accordingly.
You can put that code in the ButtonClick event or any event:
Farhan answer is the best and I would like to add that if you need to fullfil both conditions adding the OR operator works, like this:
Как проверить textbox на пустоту c
You have to replace
Then you will have the solution.
If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding.
- Proposed as answer by Syed Shakeer Hussain Saturday, January 28, 2012 6:04 AM
- Marked as answer by Leo Liu — MSFT Monday, February 6, 2012 3:43 AM
Hello,
I want a method that empty’s all textboxes when a button is clicked. When I run this program then I get an nullreference exception.
textbox1.Text = "";
this is my code. it is really strange for me to do the following:
if(textbox1.Text != "")
<
textbox1.Text = "";
>
-
Carmelo La Monica
- Proposed as answer by Heslacher Friday, January 27, 2012 11:27 PM
- Marked as answer by Leo Liu — MSFT Monday, February 6, 2012 3:43 AM
John Grove, Senior Software Engineer http://www.digitizedschematic.com/
- Proposed as answer by Heslacher Friday, January 27, 2012 11:27 PM
- Marked as answer by Leo Liu — MSFT Monday, February 6, 2012 3:43 AM
Hi SpaceLama,
If you are getting a NullReferenceException it is likely that your instance ‘textbox1’ is null. The Text property of a TextBox can be set to a null value, which in turn actually sets string.Empty for that property.
You can validate this by doing something like:
You need to debug the root cause of why your textbox is null; fix that and the issue will go away. As far as setting all textbox controls to an empty string in your form, you can do something like:
Check if TextBox Is Empty in C#

This tutorial will discuss how to check if a text box is empty or not in C#.
Check if a TextBox Is Empty With the String.IsNullOrEmpty() Function in C#
The String.IsNullOrEmpty() function checks whether a string is null or empty or not in C#. The String.IsNullOrEmpty() function has a boolean return type and returns true if the string is either null or empty and otherwise returns false . We can use the String.IsNullOrEmpty() function on the string inside the TextBox.Text property to check whether the text inside the text box is empty or not. See the following code example.
In the above code, we checked whether the text box is empty or not with the String.IsEmptyOrNot() function in C#. We can also use the String.IsNullOrWhitespace() function to check whether there are whitespaces inside the text box or not, as shown below.
This approach also takes the whitespaces into consideration and displays the error message TEXT BOX IS EMPTY if there are only whitespaces inside the text box.
Check if a TextBox Is Empty With the TextBox.Text.Length Property in C#
The TextBox.Text.Length property gets the length of the text inside the text box in C#. We can use the TextBox.Text.Length == 0 condition inside the if statement to check if the text box is empty or not. See the following code example.
In the above code, we checked whether the text box is empty or not with the TextBox.Text.Length property in C#. This method is not recommended because it does not take whitespaces into consideration.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
Check Whether a TextBox is empty or not
You should use String.IsNullOrEmpty() to make sure it is neither empty nor null (somehow):
For practical purposes you might also consider using String.IsNullOrWhitespace() since a TextBox expecting whitespace as input probably negates any purpose, except in case of, say, letting the user pick a custom separator for stuff.
![]()
![]()
are your best options.
![]()
If one is in XAML, one can check whether there is text in a TextBox by using IsEmpty off of Text property.