嗨,我正在使用 Visual Studio 2010 C# 进行我的大学项目。我有一个 WinForms 应用程序,它有 8 个文本框。每当用户将文本框留空时,应该会弹出错误图标,并且应该在其旁边显示一个标签以显示错误消息。
当我执行下面的代码时,只有前两个错误提供程序起作用。其余的不显示。
有人可以帮我吗?
private void textBox1_Leave(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text))
{
errorProvider1.SetError(textBox1,"REQUIRED FIELD");
label12.Text = "REQUIRED FIELD";
}
else
{
errorProvider1.Dispose();
}
}
private void textBox2_Leave(object sender, EventArgs e)
{
monthCalendar1.Visible = false;
if (String.IsNullOrEmpty(textBox2.Text))
{
errorProvider2.SetError(textBox2,"REQUIRED FIELD");
label13.Text = "REQUIRED FIELD";
}
else
{
errorProvider2.Dispose();
}
}
private void textBox3_Leave(object sender, EventArgs e)
{
if (textBox3.Text=="")
{
errorProvider3.SetError(textBox3, "REQUIRED FIELD");
label14.Text = "REQUIRED FIELD";
}
else
{
errorProvider3.Dispose();
}
}
private void textBox4_Leave(object sender, EventArgs e)
{
monthCalendar1.Visible = false;
if (String.IsNullOrEmpty(textBox4.Text))
{
errorProvider4.SetError(textBox4, "REQUIRED FIELD");
label15.Text = "REQUIRED FIELD";
}
else
{
errorProvider4.SetError(textBox4, "");
}
}
private void textBox5_Leave(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox5.Text))
{
errorProvider5.SetError(textBox5, "REQUIRED FIELD");
label16.Text = "REQUIRED FIELD";
}
else
{
errorProvider5.SetError(textBox5, "");
}
}
private void textBox6_Leave(object sender, EventArgs e)
{
monthCalendar2.Visible = false;
if (String.IsNullOrEmpty(textBox6.Text))
{
errorProvider6.SetError(textBox6, "REQUIRED FIELD");
label17.Text = "REQUIRED FIELD";
}
else
{
errorProvider6.SetError(textBox6, "");
}
}