C# windows 表单检查所有条目然后显示消息框

C# windows form checking all entries then show message box

我正在制作一个 C# windows 表单来检查所有条目,当所有条目都有效时,将出现一个消息框,但我需要帮助确保所有条目都有效并显示一个消息框。做这一切可能更容易,但不妨学习如何做这一切。这是我目前所拥有的。

private void btn_submit_Click(object sender, EventArgs e)
    {
        string name = txt_name.Text;
        string email = txt_email.Text;
        string address = txt_address.Text;
        string course = txt_course.Text;
        string phone = txt_phone.Text;


        if (name.Length < 8)
        {
            txt_name.Text = "Invalid Name";
            txt_name.ForeColor = Color.Red;
        }
        else
        {
            txt_name.ForeColor = Color.Green;

        }

        if (email.Contains('@'))
        {
            if (email.Contains(".com") || email.Contains(".COM"))
            {

                txt_email.ForeColor = Color.Green;
            }
            else
            {
                txt_email.Text = "invalid Email";
                txt_email.ForeColor = Color.Red;
            }
        }
        else
        {
            txt_email.Text = "invalid Email";
            txt_email.ForeColor = Color.Red;
        }

        if (address.Length < 12)
        {
            txt_address.Text = "invalid Address";
            txt_address.ForeColor = Color.Red;
        }
        else
        {
            txt_address.ForeColor = Color.Green;
        }
       if (course.Contains("Games Design") || course.Contains("Electronics") || course.Contains("Mobile Communications") || course.Contains("GAMES DESIGN") || course.Contains("ELECTRONICS") || course.Contains("MOBILE COMMUNICATIONS"))
        {
            txt_course.ForeColor = Color.Green;
        }
        else
        {
            txt_course.Text = "invalid Course";
            txt_course.ForeColor = Color.Red;
        }

       if (phone.Length < 8)
        {
            txt_phone.Text = "invalid Phone Number";
            txt_phone.ForeColor = Color.Red;
        }
       else
        {
            txt_phone.ForeColor = Color.Green;
        }

    }

您可以在验证失败的任何地方添加一个布尔值并将其设置为 false。

private void btn_submit_Click(object sender, EventArgs e)
{
    string name = txt_name.Text;
    string email = txt_email.Text;
    string address = txt_address.Text;
    string course = txt_course.Text;
    string phone = txt_phone.Text;
    bool formIsValid = true;


    if (name.Length < 8)
    {
        txt_name.Text = "Invalid Name";
        txt_name.ForeColor = Color.Red;
        formIsValid = false;
    }
    else
    {
        txt_name.ForeColor = Color.Green;

    }

    if (email.Contains('@'))
    {
        if (email.Contains(".com") || email.Contains(".COM"))
        {

            txt_email.ForeColor = Color.Green;
        }
        else
        {
            txt_email.Text = "invalid Email";
            txt_email.ForeColor = Color.Red;
            formIsValid = false;
        }
    }
    else
    {
        txt_email.Text = "invalid Email";
        txt_email.ForeColor = Color.Red;
        formIsValid = false;
    }

    if (address.Length < 12)
    {
        txt_address.Text = "invalid Address";
        txt_address.ForeColor = Color.Red;
        formIsValid = false;
    }
    else
    {
        txt_address.ForeColor = Color.Green;
    }
   if (course.Contains("Games Design") || course.Contains("Electronics") || course.Contains("Mobile Communications") || course.Contains("GAMES DESIGN") || course.Contains("ELECTRONICS") || course.Contains("MOBILE COMMUNICATIONS"))
    {
        txt_course.ForeColor = Color.Green;
    }
    else
    {
        txt_course.Text = "invalid Course";
        txt_course.ForeColor = Color.Red;
        formIsValid = false;
    }

   if (phone.Length < 8)
    {
        txt_phone.Text = "invalid Phone Number";
        txt_phone.ForeColor = Color.Red;
        formIsValid = false;
    }
   else
    {
        txt_phone.ForeColor = Color.Green;
    }

    if (formIsValid)
    {
        //submit the form
    }
    else
    {
        MessageBox.Show("Your error message here");
    }

}

很简单,你可以引入一个布尔变量来表示状态,初始设置为true(比如IsAllValidEntries),如果输入无效则为假。并检查 End 处的变量,如果任何条件为假,则布尔变量的值也将为假;以下代码将帮助您:

private void btn_submit_Click(object sender, EventArgs e)
 {
     // definitions

     bool IsAllValidEntries = true;

     if (name.Length < 8)
     {
        //code here
         IsAllValidEntries = false;
     }
     else{  }

     if (email.Contains('@'))
     {
         if (email.Contains(".com") || email.Contains(".COM"))
         {
             // your code here
         }
         else
         {
             //code here
             IsAllValidEntries = false;
         }
     }
     else
     {
         //code here
         IsAllValidEntries = false;
     }

     if (address.Length < 12)
     {
         //code here
         IsAllValidEntries = false;
     }
     else
     {
         txt_address.ForeColor = Color.Green;
     }
     if (course.Contains("Games Design") || course.Contains("Electronics") || 
     {
         txt_course.ForeColor = Color.Green;
     }
     else
     {
         //code here
         IsAllValidEntries = false;
     }

     if (phone.Length < 8)
     {
         //code here
         IsAllValidEntries = false;
     }
     else
     {
         txt_phone.ForeColor = Color.Green;
     }

     if (IsAllValidEntries)
         MessageBox.Show("Well done");
     else
         MessageBox.Show("oooops!");    
 }

其他答案可以正常工作,但它自己的方法会更好,比如 AllEntriesValid 其中 returns 一个布尔值,如果所有条目都必须有效,则可以短路命中了一个无效条目,例如:

private bool AllEntriesValid()
{
   if (name.Length < 8)
   {
      txt_name.Forecolor = Color.Red;
      return false;
   }
   if (email.Contains('@'))
   {
      return false;
   }

  //if we get this far, no invalid entries were found, return true
  return true;
}

然后可以通过多次单击按钮来调用它,而不仅仅是您当前单击的按钮 - 而且它的可读性更高,并且可以使您的代码更加简洁!

然后在您的按钮代码中,您只需调用:

private void btn_submit_Click(object sender, EventArgs e)
{
   if (AllEntriesValid())
   {
      //do something now that everything is valid
   }
}

//编辑:按下提交太快了。