How to check if a textbox has any text in it? C#

时间:2019-03-19 15:09:48

标签: c# textbox registration statements

I'm creating a registration form, and I need to make sure that all of the fields are filled in. If they aren't, the user will get a message box when they click "register" stating that they need to fill all fields in. It would be set like this:

If textbox1, textbox2, textbox3 have text Messagebox.show "you have successfully registered" else Messagebox.show "you need to fill all fields in"

Any help would be appreciated.

3 个答案:

答案 0 :(得分:1)

尝试一下:

If(string.IsNullOrEmpty(textbox1.Text) 
   && string.IsNullOrEmpty(textbox2.Text)
   && string.IsNullOrEmpty(textbox3.Text))
{
     Messagebox.Show("You need to fill all fields in");
}
else
{
     Messagebox.Show("You have successfully registered");
}

答案 1 :(得分:0)

您可以使用此语句来检查所有文本框是否同时不为空。

if(!string.IsNullOrEmpty(textbox1.Text) && !string.IsNullOrEmpty(textbox2.Text) && !string.IsNullOrEmpty(textbox3.Text))
 {
        //display your registration message
 }

答案 2 :(得分:0)

您应该给出一些您已经尝试过的代码,但这是这样的:

if(textbox1.Text.Length > 0 && textbox2.Text.Length > 0 && textbox3.Text.Length > 0) 
{
    MessageBox.Show("you have successfully registered");
}else{
    MessageBox.Show("you need to fill all fields in");
}