views:

309

answers:

1

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-messages 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);
    }
}
A: 

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 can't 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...

Andrew.

Reddog
Well, that works, but unfortunately not with databinding used...
BeowulfOF
@BeowulfOF - I'm not sure what you mean? The regular ErrorProvider supports validation of databound properties by implementing IDataErrorInfo on your bound object. You will need to set the DataSource property of the ErrorProvider to the same BindingSource used in the control's binding.
Reddog
done that, but padding and alignment is not used from the subclassed errorprovider..
BeowulfOF
@BeowulfOF - As per answer, you will find that the alignment/padding is usually set in the .designer.cs file so you'll have to open the designer or search for your SetIconAlignment statements in there to remove them or else open the Winforms Designer whereby they will hopefully be reset to the default that we redefined in our new subclass.
Reddog