What is the standard approach for event handling i

2019-05-29 14:42发布

问题:

We know that when a Form is visually inherited, all of its controls are locked in design time. This problem requires that we place event handlers in the base form. Now what should I do if I want to place base-control event handlers in the derived Form?

Since the controls are locked, approach of double clicking on the control to add an event-handler should not work.

What is the industry-standard approach for event handling in case of Visual Inheritance?

回答1:

The designer honors the access modifiers on the base class members. You must change the Modifiers property of the base form's control from Private to Protected. Recompile. Now the inherited form has access to the control, you'll have no trouble overriding properties and assigning an event handler from the designer.



回答2:

You handle the event on the base Form and use it to call a virtual method. The derived Form overrides that method.



回答3:

Another method is to add an event in the user control (instead of a virtual method) and then fire it when the internal event is fired.