Can you just set the focus if you are adding the control to the errors list?
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);
}
But I think the only way to do this properly is if you can use a boolean flag field that you can set to false
just before you call the method that causes SetError()
to be called repeatedly.
I mean something like this:
private boolean _isFirstError;
And just before you start verifying set _isFirstError = true
and then in 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);
}