2010-05-19 64 views
3

我們使用派生的Form-Classes,其中一個基本窗體類用於我們的軟件。如何以編程方式爲所有控件設置ErrorProvider圖標

在派生形式上,我們廣泛使用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); 
    } 
} 

回答

1

我們使用一種遺傳ErrorProvider組件,而不是其強行設置/返回的IconAlignment擴展屬性的默認值。

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 
} 

然後,你可以很容易地做一個查找/替換爲new ErrorProvider()new MyErrorProvider()取代。

我不記得確切,但你會發現,你可能需要爲了得到它重新序列化傳遞到在form.designer.cs文件SetIconAlignment值來打開窗體的設計...

+0

好了,這樣的作品,但不幸的是不使用數據綁定... – 2010-05-28 06:37:59

+0

@BeowulfOF - 我不確定你的意思?常規的ErrorProvider通過在綁定對象上實現IDataErrorInfo來支持數據綁定屬性的驗證。您需要將ErrorProvider的DataSource屬性設置爲控件綁定中使用的相同BindingSource。 – Reddog 2010-05-28 19:58:16

+0

做到了這一點,但填充和對齊不是從子類錯誤提供程序中使用的。 – 2010-05-29 08:43:48

相關問題