How to set ErrorProvider Icon left for all Controls programmatically
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-me开发者_Python百科ssages 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);
}
}
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...
精彩评论