您好我正在我的大学项目上的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, "");
}
}
尝试了您的soltion但它没有工作了我。你有任何替代解决方案??感谢努力neway – Brian
更具体。什么没有奏效?编译错误?运行时错误?不是期望的效果?你是否已将所有错误提供者控件更改为一个,并将它们全部更名?我只显示了一个重命名... – okrumnow
没有得到预期的效果。我用所有的errorproviders分别 – Brian