4

我们使用派生的表单类,我们的软件有一个基本表单类。

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

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

欢迎任何提示...

设置 IconAlignment 的代码:

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

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

1 回答 1

1

我们使用继承的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().

我记不清了,但您可能会发现您可能需要打开表单的设计器才能使其重新序列化传递到文件SetIconAlignment中的值form.designer.cs...

于 2010-05-19T19:51:54.807 回答