简体   繁体   English

检查 TextBox 是否为空

[英]Check Whether a TextBox is empty or not

I have a TextBox.我有一个文本框。 And I want to check if it's empty.我想检查它是否为空。

Which way is better哪种方式更好

if(TextBox.Text.Length == 0)

or或者

if(TextBox.Text == '')

? ?

You should use String.IsNullOrEmpty() to make sure it is neither empty nor null (somehow):您应该使用String.IsNullOrEmpty()来确保它既不是空也不是空(不知何故):

if (String.IsNullOrEmpty(textBox1.Text))
{
    // Do something...
}

More examples here .更多例子在这里

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.出于实际目的,您还可以考虑使用String.IsNullOrWhitespace()因为期望空格作为输入的 TextBox 可能会否定任何目的,除非让用户为内容选择自定义分隔符。

I think我认为

string.IsNullOrEmpty(TextBox.Text)

or或者

string.IsNullOrWhiteSpace(TextBox.Text)

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.如果是 XAML,则可以使用Text属性中的IsEmpty来检查TextBox是否有Text

Turns out that it bubbles down to CollectionView.IsEmpty (not on the string property) to provide the answer.结果是它冒泡到CollectionView.IsEmpty (不在字符串属性上)以提供答案。 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.凡在第二个文本框(水印的)风格将绑定到Text主文本框,打开/关闭相应。

<TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding ElementName=tEnterTextTextBox, Path=IsKeyboardFocusWithin}" Value="False" />
                    <Condition Binding="{Binding ElementName=tEnterTextTextBox, Path=Text.IsEmpty}" Value="True" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Visibility" Value="Visible" />
            </MultiDataTrigger>
            <DataTrigger Binding="{Binding ElementName=tEnterTextTextBox, Path=IsKeyboardFocusWithin}" Value="True">
                <Setter Property="Visibility" Value="Hidden" />
            </DataTrigger>
            <DataTrigger Binding="{Binding ElementName=tEnterTextTextBox, Path=Text.IsEmpty}" Value="False">
                <Setter Property="Visibility" Value="Hidden" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>

Another way:其它的办法:

    if(textBox1.TextLength == 0)
    {
       MessageBox.Show("The texbox is empty!");
    }

You can put that code in the ButtonClick event or any event:您可以将该代码放在ButtonClick事件或任何事件中:

//Array for all or some of the TextBox on the Form
TextBox[] textBox = { txtFName, txtLName, txtBalance };

//foreach loop for check TextBox is empty
foreach (TextBox txt in textBox)
{
    if (string.IsNullOrWhiteSpace(txt.Text))
    {
        MessageBox.Show("The TextBox is empty!");
        break;
    }
}
return;

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: Farhan 的答案是最好的,我想补充一点,如果您需要满足这两个条件,添加 OR 运算符可以工作,如下所示:

if (string.IsNullOrEmpty(text.Text) || string.IsNullOrWhiteSpace(text.Text))
{
  //Code
}

Note that there is a difference between using string and String请注意,使用stringString是有区别的

In my opinion the easiest way to check if a textbox is empty + if there are only letters:在我看来,检查文本框是否为空的最简单方法+是否只有字母:

public bool isEmpty()
    {
        bool checkString = txtBox.Text.Any(char.IsDigit);

        if (txtBox.Text == string.Empty)
        {
            return false;
        }
        if (checkString == false)
        {
            return false;
        }
        return true;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM