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

C# 如何以编程方式为所有控件设置ErrorProvider图标,c#,winforms,errorprovider,C#,Winforms,Errorprovider,我们使用派生形式类,软件使用一个基本形式类 在派生表单上,我们广泛使用数据绑定来处理业务对象,所有这些都实现了IDataErrorInfo,并将错误输入上的自定义错误消息通过ErrorProviders发送到GUI 现在,我正在寻找一种方法来实现基本表单类中的函数,以获取表单上的所有ErrorProvider组件,并将表单上每个控件的IconAlignment设置为左侧(因为右侧是一个间距问题) 任何提示欢迎 设置IconAlignment的代码: private void SetErrorPr

我们使用派生形式类,软件使用一个基本形式类

在派生表单上,我们广泛使用数据绑定来处理业务对象,所有这些都实现了IDataErrorInfo,并将错误输入上的自定义错误消息通过ErrorProviders发送到GUI

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

任何提示欢迎

设置IconAlignment的代码:

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

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

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

例如

然后您可以轻松地搜索/替换
newerrorprovider()
,并替换为
newmyerrorprovider()


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

@BeowulfOF-我不确定您的意思是什么?常规ErrorProvider通过在绑定对象上实现IDataErrorInfo来支持数据绑定属性的验证。您需要将ErrorProvider的DataSource属性设置为控件绑定中使用的相同BindingSource。请完成此操作,但子类ErrorProvider中不使用填充和对齐方式。@BeowulfOF-根据答案,您会发现对齐/填充通常在.designer.cs文件中设置,因此您必须打开设计器或在其中搜索SetIconAlignment语句以删除它们,或者打开Winforms设计器,以便将它们重置为我们在新子类中重新定义的默认值。搜索出现任何“ErrorProvider”并决定是否必须更改。(例如,某些铸件(如有)可能无法按预期工作)。
[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
}