Visual Inheritance with C#

Microsoft VS.NET provides design-time support for form inheritance. A derived form can be added to your solution by right-clicking on your project in the Solution Explorer window and choosing “Add…” and then “Add Inherited Form” from the context menu. After your derived form and its code behind are generated, you may need to change some code.

Assume that you are the developer of both base and derived forms. The VS.NET Form Designer may generate code for a control specific event handler, event-handling method, and register it with the control’s event.

For example, an event handler generated for a button’s Click might look like this:

   private void button1_Click(object sender,      System.EventArgs e) {. . .}

The code to register (subscribe) an event handler instance for an event was added to the “Designer generated code” code region:

   this.button1.Click += new      System.EventHandler(this.button1_Click);

If you want to reuse the base form button1 control in the derived form, you need to do some code changes:

  • Change the Modifiers “private” (default) value to “protected” value on the base form’s control property page to indicate your intention to make it accessible from the derived classes.
  • Specify an access modifier for the button1_Click() event handler in the base form’s class according to your design.
  • Add the “virtual” key word to the button1_Click() event handler declaration in the base form’s class to support polymorphism.
  • Add the “new” or “override” keyword to the button1_Click() event handler in the derived form’s class to specify your design intention.

Now, you should make your next important design decision regarding the run-time calls to the button1_Click() event handlers. The VS.NET Form Designer, by default, registered the button1_Click() event handler for the base and derived form classes. You have the following options for organizing their execution:

  • If you want both handlers to be called (base will be called first), leave it as it is.
  • If you do not want the base class’ event handler to be called, remove its registration. Also, you may try programmatically unregistering the base class event handler in your derived class using the classes in the Reflection namespace. Note that it can be the only option if you do not have access to the base code or legal permission to modify it.
  • If you use the “override modifier,” and you want the OO way of calling the handler, remove its registration from the derived class. Note that you still may call the base class’ event handler from inside the derived class’ event handler if you need.

The following code snippet demonstrates a use of the classes in the Reflection namespace in the derived form’s class:

   System.Reflection.EventInfo baseEventHndlr =      (typeof(ClassLibrary2.Form1)).GetEvent("Click");   System.Reflection.MethodInfo removeMethod =       baseEventHndlr.GetRemoveMethod();   removeMethod.Invoke(base.button1,      new Object[]{new System.EventHandler         (base.button1_Click)}); 

This code can be executed after the base class’ constructor has been executed. This code works if the button1 base form’s control and its button1_Click() event handler are accessible.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may