Проверьте, является ли TextBox пустым или нет
У меня есть TextBox. И я хочу проверить, пусто ли оно.
Какой путь лучше
5 ответов
Вам следует использовать String.IsNullOrEmpty() , чтобы убедиться, что он не пуст и не равен нулю (каким-то образом):
Дополнительные примеры можно найти здесь.
В практических целях вы также можете рассмотреть возможность использования String.IsNullOrWhitespace() , поскольку TextBox, ожидающий пробелов в качестве ввода, вероятно, сводит на нет любую цель, за исключением, скажем, случая, когда пользователь может выбрать собственный разделитель для материала.
Если один находится в XAML, можно проверить, есть ли текст в TextBox , используя IsEmpty off свойства Text .
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.
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 в visual c++
НЕ ПУТАЙТЕ С++ И С ШАПР
Валера написал один с способов проверки.
Чтобы пробели также считались пустой нужно обвернуть тексбокс1.текс в функцию Trim
В .NET Framework 4, 4.5 и 4.5.1 (это Visual Studio версии 2010 и выше) можно вот так
http://msdn.microsoft.com/ru-ru/library/system.string.isnullorwhitespace(v=vs.110).aspx
В более старых Visual Studio можно считать строку, пройти по ее символам в цикле for, и проверить каждый символ, не будет ли он пустым.