Visual Inheritance with C#

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.
See also  Monetize TikTok For Your Business

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.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist