Click here to Skip to main content
Click here to Skip to main content
Go to top

Creating Template Forms using Visual Inheritance

, 13 May 2004
Rate this:
Please Sign up or sign in to vote.
Understanding visual inheritance in .NET and creating generic Template forms using visual inheritance.

Introduction

As we know, in OOP’s, Inheritance is the ability to use all of the functionality of an existing class, and extend those capabilities without re-writing the original class. Basically, it means one object to inherit another object’s methods and properties.

In .NET, inheritance is not just limited to designing classes but also extended to visual designing. So, what does it mean? Well, it means we can use inheritance in Form designing too, so this kind of usage is called Visual Inheritance.

But Visual Inheritance of Windows Forms is still inheritance based on classes. One has to keep in mind all other inheritance features while using visual inheritance. For example, if you create methods and properties in base classes, one has to take care which of them can be overridden by derived classes.

One of the main advantages of visual inheritance is it reduces or cuts down the development time, and helps in designing consistency in the Forms layouts.

In most of the application development, you find common appearance in the form layout. Technically speaking, it’s basically a Form Templates concept followed in the application design to give the same appearance throughout the product.

Most of the developers spend their time in designing the form layouts during the application development. So, with the usage of visual inheritance, developers can cut down the time & cost.

Following below are some of the areas where you can use visual inheritance in form designing:

  • Forms having common menu structure
  • Forms having common graphic images and buttons
  • Form's behavior has to be changed depending on usage
  • Forms which require common code

Using visual inheritance, however, you could create a Template form containing all your common controls, along with common code .Then, you can derive new forms from your Template base form that provide the appropriate functionality.

Let me explain this with a small example:

In the sample application, we will assume most of the forms should have same look and feel in UI. So, let’s create a Template form which has a menu bar, logo and buttons which will be common for all the forms. Figure.1 shows the Template form.

Template.vb

Figure [1]. Template.vb

In the sample application Visual Inherit, we will create a Form inheriting from Template.vb. Figure [2] shows the inherited form called FormsUsingTemplate.vb.

FormsUsingTemplate.vb

Figure [2]. FormsUsingTemplate.vb

To inherit from Template.vb form, all you need to do is right click project’s Solution Explorer and select the “Add New Item” and then select “Inherited Form”. Once you select “Inherited Form”, you will see an Inheritance Picker dialog box showing all the forms available in the project, and we will select Template Form for our sample, as shown in below Figure [3].

Inheritance Picker

Figure [3]. Inheritance Picker

In code view, you can see that FormsUsingTemplate form is derived from Template Form.

Public Class FormsUsingTemplate
      Inherits VisualInherit.frmTemplate

And if you see in design view, FormsUsingTemplate form, all the inherited items from base templates is shown with special icon on the Form Items. These cannot be edited in the inherited form. If you need to edit any visual appearance of the template, then you need to edit in Template form which will be automatically reflected in derived form.

So, now you can add all required controls in the derived form, and if required, one can even override some of the methods in the base form by manually changing the code, modifiers and also methods depending on your requirement. In the sample application, I just shadowed the button click events in the derived form.

Private Shadows Sub butSubmit_Click(ByVal sender _
  As System.Object, ByVal e As System.EventArgs) Handles butSubmit.Click

  Dim msg As String
  msg = "Name :" & txtName.Text & " " &_
        "Age :" & " " & txtAge.Text & " " &_
        "Salary :" & txtSalary.Text
  MessageBox.Show(msg)
End Sub

Conclusion

Visual Inheritance helps in cutting down the development time, and also, just like inheritance in classes as language feature, inheritance can be used in forms designing as well.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Chandra Hundigam
Architect
United States United States
Chandra Hundigam has Master degree in Computer Application, Microsoft Certified Professional and Software Architect. He's significantly involved in enterprise application development and distributed object oriented system development using Microsoft .Net, Sun Java/J2EE technology to serve global giants in the Media, Finance, Mortgage and Software Industries.Presently working as Independent Software Consultant for a US-based company.His areas of interests are in emerging Technologies.

Comments and Discussions

 
Questioninheritance restrictions Pinmemberkaimn_ryu10-May-12 2:55 
AnswerRe: inheritance restrictions Pinmembertyler.durden4043-Sep-14 3:11 
Generaldatabase help Pinmemberfaisalrana14-Nov-07 8:40 
GeneralRe: database help PinmemberPaszt17-Oct-08 12:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 14 May 2004
Article Copyright 2004 by Chandra Hundigam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid