constraining an ExtenderProvider ... Winform Component
-
My intention is to publish, here, a tutorial on extending 'Form with a Component in what I hope will be a novel, and useful, way. As you may know, a component that implements IExtenderProvider (see: [^]) can be drag-dropped into the component UI area of the Form, and will be exposed at design-time by the type of object it extends. Note that the MSDN sample implementation here: [^] ... as so often, brain-damaged with crap code ... is a Control, not a Component. Okay, now I have my Component working, extending 'Form; I have solved the problem of detecting the Component instance's run-time Container using reflection (the only way to do it !). Necessary Properties are now exposed in the design-time 'PropertyGrid. Depending on the user's design-time choice, one of four possible "flavors" of another Form are created. All this is working. The issue now ... is that I want to constrain the number of auxiliary Forms created to only one of each of the four "flavors." AFAIK, this cannot occur at design-time in the Component's code: there aren't any Events, and the 'InitializeComponent method does not get called. So, which do you think is better: handle preventing duplicates in a special static Class, or handle them in the Form 'Load Event when I enumerate the Components, and take actio based on the user-set Properties ? thanks, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
My intention is to publish, here, a tutorial on extending 'Form with a Component in what I hope will be a novel, and useful, way. As you may know, a component that implements IExtenderProvider (see: [^]) can be drag-dropped into the component UI area of the Form, and will be exposed at design-time by the type of object it extends. Note that the MSDN sample implementation here: [^] ... as so often, brain-damaged with crap code ... is a Control, not a Component. Okay, now I have my Component working, extending 'Form; I have solved the problem of detecting the Component instance's run-time Container using reflection (the only way to do it !). Necessary Properties are now exposed in the design-time 'PropertyGrid. Depending on the user's design-time choice, one of four possible "flavors" of another Form are created. All this is working. The issue now ... is that I want to constrain the number of auxiliary Forms created to only one of each of the four "flavors." AFAIK, this cannot occur at design-time in the Component's code: there aren't any Events, and the 'InitializeComponent method does not get called. So, which do you think is better: handle preventing duplicates in a special static Class, or handle them in the Form 'Load Event when I enumerate the Components, and take actio based on the user-set Properties ? thanks, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
Sounds like a "business rule" being baked in; maybe it should be a run-time option; which means at "load time"; preferably with a message if the rules are violated.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
Sounds like a "business rule" being baked in; maybe it should be a run-time option; which means at "load time"; preferably with a message if the rules are violated.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
Thanks, Gerry, I like the idea of a run-time warning; but, I am curious how, in general, one can detect the drag-drop of a Component on a Form at design-time. It appears that's impossible. This is the code I use to find the custom Extender Provider using the Type of the Container Form:
public IEnumerable EnumerateComponents(Type formtype, string targetname)
{
return formtype.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(finfo =>
finfo != null
&& typeof(IExtenderProvider).IsAssignableFrom(finfo.FieldType)
&& finfo.FieldType.Name == targetname);
}cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot