Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Template Forms using Visual Inheritance

0.00/5 (No votes)
13 May 2004 1  
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