I’m experimenting around with writing custom WinForms components and I wrote a couple of simple validator components for use with a subclass of ErrorProvider that automatically hooks up validation events. All these components can be added to a form and hooked up using only the designer, thanks to IExtenderProvider.
Now in trying to go up one level, I’m trying to get a composite validator to be usable with the designer. I can get it up and working with code, but that’s really easy. I’d like to get it to work in a designer-only way.
My difficulty resides in exposing a property that is a collection of other validators that are in the same form. The validators all inherit directly from Component, and implement a IControlValidator interface. I’m open to changing this to have them inherit from a ValidatorComponent base class if it helps.
I thought of a couple solutions, but either I don’t like them, or I can’t get them to work:
-
Make the validators into invisible controls, and the have composite validator contain them, similar to what a
Paneldoes;This one I don’t like because it is more of a hack, and having to juggle them among true controls just feels wrong;
-
Use a collection editor, as you use for toolbars;
I looked around the web and found a couple of articles about this, but I couldn’t get it to work. At least without building my own editor form, which would be too much of a hassle for an experiment project.
I admit I didn’t spend much time trying this, because I realized using the standard
CollectionEditorwould lock me down to using a fixed set of validator types (it would, wouldn’t it?).I also thought of creating a simple
ValidatorReferenceclass with a single property of typeIControlValidatorand use that as the element type for a simple collection editor. I would then add one of these, and in its property grid set the property to an existing validator component. This one seems easy to get working, but loses its appeal because it is such an obvious hack.
Anyone has any other ideas? Is there something I’m missing and this is actually something simple?
Why not creating an editor to do this???
You think it sounds an overkill, but actually it is not.
I will demonstrate with a sample.
Sample description
In this sample I will be creating a control named
ButtonActivityControlthat is abled to make multiple references to other controls in the same form, using a property calledButtons, that is an array of type Button (i.e.Button[]).The property is marked with a custom editor, that makes it easy to reference the controls in the page. The editor shows a form that consists of a checked list box, that is used to select multiple controls that are in the very same form.
Steps to create the sample
1) a Form called ReferencesCollectionEditorForm
Code of ReferencesCollectionEditorForm:
2) an UITypeEditor
Code of ReferencesCollectionEditor:
3) a control that uses other controls in the same form
Code of custom control:
Now create a form that will contain the custom control, place some buttons on it, and then place a ButtonActivityControl on it. The custom control has a property called Buttons, that is editable.
That’s it!!
No reason to fear custom Editors… and not so complex….
dit it in half an hour.
I think this is the answer… that is, I think it is! =) Maybe I didn’t understand the question well… but thats the best one can do: trying to help others!
EDIT
This code is needed in the ReferencesCollectionEditor: