gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:43 26 4
gpt4 key购买 nike

我正在制作一个 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;
}

}

最佳答案

您可以在验证失败的任何地方添加一个 bool 值并将其设置为 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");
}

}

关于C# windows 窗体检查所有条目然后显示消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com