messagebox显示所有验证字段

时间:2017-01-06 05:30:23

标签: c# textbox messagebox isnullorempty

所以我有3个字段组合成1个字符串,而我目前正在修复验证,所以问题是如何识别某个空文本框,用户需要在他/她可以填写之前填写它继续我试过这个

 if(string.IsNullOrEmpty(txtEYear.Text) || string.IsNullOrEmpty(txtECat.Text) || string.IsNullOrEmpty(txtEID.Text))
                {
                    MessageBox.Show("Please fill in the missing fields");
                }

2 个答案:

答案 0 :(得分:1)

为此,您必须使用单独的循环并形成如下消息:

bool isValidated = true;
StringBuilder message= new StringBuilder("Please fill the following fields: ");
if(string.IsNullOrEmpty(txtEYear.Text) 
{
  message.Append("Year");
  isValidated = false;
}
if(string.IsNullOrEmpty(txtECat.Text))
{
   message.Append("txtECat");
   isValidated = false;
} 
if(string.IsNullOrEmpty(txtEID.Text))
{
  message.Append("ID");
  isValidated = false;
}

// check all fields are valid
if(isValidated)
{
  // Continue 
}
else
{
    MessageBox.Show(message.ToString());
}                  

答案 1 :(得分:1)

尝试这个。当任何字段为空时,请关注所需的字段。

string message = string.empty;
message = "Please fill the ";
if(string.IsNullOrEmpty(txtEYear.Text))
    {
       message = message + " txtEYear ";
       txtEYear.Focus();
    }
    if(string.IsNullOrEmpty(txtECat.Text))
    {
       message = message + " txtECat";
       txtECat.Focus();
    }
    if(string.IsNullOrEmpty(txtEID.Text))
    {
       message = message + " txtEID";
       txtEID.Focus();
    }

    MessageBox.Show(message+" Fields");