代码之家  ›  专栏  ›  技术社区  ›  Kevin Vaughan

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

  •  3
  • Kevin Vaughan  · 技术社区  · 16 年前

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

    设置图标对齐的代码:

    private void SetErrorProviderIconAlignment(ErrorProvider errorProvider, Control control)
    {
        errorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
    
        foreach (Control subControl in control.Controls)
        {
            SetErrorProviderIcon(errorProvider, subControl);
        }
    }
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   pb2q    13 年前

    我们用的是遗传的 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 文件夹。。。