在表单中,我有一个12个控件。(所有控件都应该填充一些数据),如果用户想要保存,则不向控件输入任何文本,我将向所有控件显示ErrorProviders。请输入数据。我在给你看密码
public ErrorProvider mProvider;
public void SetError(Control ctl, string text)
{
if (string.IsNullOrEmpty(text)) mErrors.Remove(ctl);
else if (!mErrors.Contains(ctl)) mErrors.Add(ctl);
mProvider.SetError(ctl, text);
ctl.Focus();
}如果控件有空数据,则将控制信息和错误文本传递给SetError方法。我想将focus()设置为第一个控件,该控件访问此SetError方法。
点击按钮,我就调用这个方法
Public void Isinptvlid
{
if (textBox1.Text.Length == 0)
{
obj.SetError(textBox1, "textBox1 cann't be Zero Length");
}
if (textBox2.Text.Length == 0)
{
obj.SetError(textBox2, "textBox2 cann't be Zero Length");
}
if (textBox3.Text.Length == 0)
{
obj.SetError(textBox3, "textBox3 cann't be Zero Length");
}
if (textBox4.Text.Length == 0)
{
obj.SetError(textBox4, "textBox4 cann't be Zero Length");
}
if (textBox5.Text.Length == 0)
{
obj.SetError(textBox5, "textBox5 cann't be Zero Length");
}
if (textBox6.Text.Length == 0)
{
errprvBase.SetError(textBox6, "textBox6 Cann't be Zero Length");
}
if (textBox7.Text.Length == 0)
{
errprvBase.SetError(textBox7, "textBox7 Cann't be Zero Length");
}
}发布于 2013-05-24 09:53:04
如果要将控件添加到错误列表中,可以只设置焦点吗?
public void SetError(Control ctl, string text)
{
if (string.IsNullOrEmpty(text))
{
mErrors.Remove(ctl);
}
else if (!mErrors.Contains(ctl))
{
mErrors.Add(ctl);
ctl.Focus();
}
mProvider.SetError(ctl, text);
}但是,我认为正确做到这一点的唯一方法是,在调用导致重复调用false的方法之前,可以使用一个布尔标志字段,该字段可以设置为SetError()。
我的意思是这样的:
private boolean _isFirstError;在您开始验证set _isFirstError = true之前,然后在SetError()中
public void SetError(Control ctl, string text)
{
if (string.IsNullOrEmpty(text))
{
mErrors.Remove(ctl);
}
else if (!mErrors.Contains(ctl))
{
mErrors.Add(ctl);
if (_isFirstError)
{
_isFirstError = false;
ctl.Focus();
}
}
mProvider.SetError(ctl, text);
}发布于 2013-05-24 09:51:42
设置窗体的ActiveControl属性。
public ErrorProvider mProvider;
public void SetError(Control ctl, string text)
{
if (string.IsNullOrEmpty(text)) mErrors.Remove(ctl);
else if (!mErrors.Contains(ctl)) mErrors.Add(ctl);
mProvider.SetError(ctl, text);
ActiveControl = ctl;
}https://stackoverflow.com/questions/16732075
复制相似问题