I have the following forms in my winforms framework
- FormBase (inherited from Form)
- FormBaseList (inherited from FormBase)
- FormBaseDetail (inherited from FormBase)
Now every form in the application inherits from on one of the 3 above.
For example FormCustomerList will be inherited from FormBaseList
Now in FormBaseList the event FormBaseList_Shown is present (by doubleclicking on it in the properties window in VS)
What I would like to know in the code from FormBaseList_Show is if there is an event FormCustomerList_Show present (again by doubleclick on it in the properties window).
Is that even possible ?
So why do I want this ?
Because some changes in the framework require the forms to not use the Shown event anymore but a custom event.
I would like to catch and show a warning to the developer if he adds a Show event to a form, and if it is really needed he can set a property that will hide this warning.
This warning does not needs to be shown at designtime, at runtime would be enough. But if its possible at designtime that would be a bonus.
So can this be done and is there maybe a better way to do this ?
I hope this explanation is clear
EDIT
The idea is that when a developer makes use of a Show event he must get a warning (either at designtime or runtime). If he feels that he really needs the Show method he should be able to set the warning off for this particular form
To throw exception or show a message box at run-time you have the following options:
Shownevent and in theaddpart, throw an exception (unless the skip flag has been set).Shownevent and check if there is a handler attached to the event.In both solutions, a boolean property can be used to override the behavior in derived forms.
Option 1 - Shadowing Shown event and add the code to
addYou can shadow the
Shownevent and in theaddaccessor, add a code to show a message box or throw exception if a handler added to the event.In the following example, I've added
ThrowExceptionOnSubscribingShownEventproperty to the base form which istrueby default which means it throws the exception on subscribing theShownevent.Option 2 - Finding event handler list for
ShowneventAs an option for run-time, you can override
OnShownmethod and using reflection, getEVENT_SHOWNfield and using it, get the event handler list ofShownevent. Then you can check if the event handler list is not empty, throw an exception.In the following example, I've added
ThrowExceptionOnSubscribingShownEventproperty to the base form which istrueby default which means it throws the exception on subscribing theShownevent. You can set it tofalsewhen needed in derived forms: