-1

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

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

2 回答 2

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());
}                  
于 2017-01-06T05:36:36.147 回答
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");
于 2017-01-06T05:36:40.003 回答