如何以编程方式为所有控件设置 ErrorProvider 图标

发布于 2024-09-02 19:54:47 字数 619 浏览 4 评论 0原文

我们使用派生的表单类,并为我们的软件提供一个基本表单类。

在派生表单上,我们广泛使用 DataBinding 来处理 BusinessObjects,所有这些都实现 IDataErrorInfo,通过 ErrorProviders 将自定义错误消息抛出到 GUI 的错误输入上。

我现在寻找一种在基本表单类中实现函数的方法,以获取表单上的所有 ErrorProvider-Components 并将表单上每个控件的 IconAlignment 设置为左​​侧(因为右侧是间距问题)。

欢迎任何提示...

设置图标对齐的代码:

private void SetErrorProviderIconAlignment(ErrorProvider errorProvider, Control control)
{
    errorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);

    foreach (Control subControl in control.Controls)
    {
        SetErrorProviderIcon(errorProvider, subControl);
    }
}

We use derived Form-Classes, with one base form class for our software.

On the derived forms we use DataBinding extensively to deal with our BusinessObjects, all implementing IDataErrorInfo, throwing custom error-messages on false inputs to the GUI with ErrorProviders.

I now search for a way to implement a function in the base-form-class to get all ErrorProvider-Components on a Form and set the IconAlignment for every Control on the Form to left (since right is a spacing-problem).

Any Hints welcome...

Code for Setting the IconAlignment:

private void SetErrorProviderIconAlignment(ErrorProvider errorProvider, Control control)
{
    errorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);

    foreach (Control subControl in control.Controls)
    {
        SetErrorProviderIcon(errorProvider, subControl);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-09-09 19:54:47

我们使用继承的 ErrorProvider 组件来强制设置/返回 IconAlignment 扩展属性的默认值。

例如,

[ToolboxBitmap(typeof(ErrorProvider))]
[ProvideProperty("IconAlignment", typeof(Control))]
public class MyErrorProvider : ErrorProvider
{
    #region Base functionality overrides

    // We need to have a default that is explicitly different to 
    // what we actually want so that the designer generates calls
    // to our SetIconAlignment method so that we can then change
    // the base value. If the base class made the GetIconAlignment
    // method virtual we wouldn't have to waste our time.
    [DefaultValue(ErrorIconAlignment.MiddleRight)]
    public new ErrorIconAlignment GetIconAlignment(Control control)
    {
        return ErrorIconAlignment.MiddleLeft;
    }

    public new void SetIconAlignment(Control control, ErrorIconAlignment value)
    {
        base.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
    }

    #endregion
}

然后您可以轻松搜索/替换 new ErrorProvider() 并替换为 new MyErrorProvider()

我记不清了,但您可能会发现您可能需要打开表单的设计器,以便让它重新序列化传递到 form.designer.csSetIconAlignment 的值> 文件...

We used an inherited ErrorProvider component instead which forcefully set/returned the default for the IconAlignment extended property.

E.g.

[ToolboxBitmap(typeof(ErrorProvider))]
[ProvideProperty("IconAlignment", typeof(Control))]
public class MyErrorProvider : ErrorProvider
{
    #region Base functionality overrides

    // We need to have a default that is explicitly different to 
    // what we actually want so that the designer generates calls
    // to our SetIconAlignment method so that we can then change
    // the base value. If the base class made the GetIconAlignment
    // method virtual we wouldn't have to waste our time.
    [DefaultValue(ErrorIconAlignment.MiddleRight)]
    public new ErrorIconAlignment GetIconAlignment(Control control)
    {
        return ErrorIconAlignment.MiddleLeft;
    }

    public new void SetIconAlignment(Control control, ErrorIconAlignment value)
    {
        base.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
    }

    #endregion
}

Then you could easily do a search/replace for new ErrorProvider() and replace with new MyErrorProvider().

I cannot recall exactly, but you may find that you may need to open the Form's designer in order to get it to reserialize the value passed into SetIconAlignment in the form.designer.cs files...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文