【问题标题】:How to give focus() to the first control that Error Provider hits in C# Windows Application?如何将焦点()赋予错误提供程序在 C# Windows 应用程序中命中的第一个控件?
【发布时间】:2013-05-19 21:13:10
【问题描述】:

在一个表单中,我有 12 个控件。 (所有控件都应该填充一些数据)如果用户想要SAVE,无需在控件中输入任何文本,我将向我的所有控件显示 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");
    }
}

【问题讨论】:

  • 什么是'mErrors'!?

标签: c# winforms focus controls errorprovider


【解决方案1】:

如果您将控件添加到错误列表中,您可以设置焦点吗?

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);
}

但我认为正确执行此操作的唯一方法是,如果您可以在调用导致重复调用 SetError() 的方法之前使用可以设置为 false 的布尔标志字段。

我的意思是这样的:

private boolean _isFirstError;

就在您开始验证集合_isFirstError = trueSetError() 之前:

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);
}

【讨论】:

  • 点击按钮我正在检查任何控件的文本是否为空。如果全部为空,则我向所有控件显示错误提供程序,但我想将焦点显示到第一个控件哪个错误提供程序首先设置错误
【解决方案2】:

设置表单的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;
    }

【讨论】:

  • 没有 ActiveControl 属性
  • 在您的表格上?如果它是 Systems.Windows.Forms.Form 的后代,肯定有。
  • System.windows.Forms 命名空间在那里
  • 这段代码来自哪个类?它是形成一种形式还是来自其他东西?
猜你喜欢
  • 1970-01-01
  • 2011-01-06
  • 2011-05-02
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多