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.

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of