Visual Inheritance with Windows Forms
Inheritance is an important feature of Object oriented Programming language, using which we can reuse code and extend the functionality of a class. Because OOPS technology is applied to visual elements like buttons, text boxes or any controls in a windows form, inheriting of the visual elements is referred to as visual inheritance.
Why to use Visual Inheritance ? To create consistent windows forms in vb.net and c#, we use visual inheritance. Visual inheritance allows us to reuse the windows form and also to extend the functionality in multiple forms.
To implement Visual Inheritance, we create a windows Form (base form ) and inherit forms from the base form. This is known as Form inheritance. The advantage of using this feature is that when changes are made to the base form, all the inherited forms will automatically adapt the changes.
A typical Accounting module of an ERP software consists of data entry forms to enter Cash Voucher data, Cheque Voucher data and Receipt data. All these data entry forms have similar user interface. We will consider a simple example to demonstrate Visual Inheritance feature. In Visual Basic 2008, we create a base form in a project and then create another form that inherits from the base form.
How to override base form methods in inherited form - Visual Inheritance The inherited form inherits all the features of the base form. In addition, it can override the base form Overridable methods declared with Protected or Public access modifiers. Since the requirement is to create a similar user interface for all the data entry forms, we create a base form that has the common controls and buttons. We can then inherit all the data entry forms from the base form. To provide a meaningful name to the form, set the form name as 'BaseForm' and the text as 'BaseForm'. The following are the some of the controls in the base form and their properties.
Add the following code in the BaseForm.
Creating an inherited form using Visual Inheritance After the base form is designed, we can create an inherited form. First build the application by selecting the build option. Click on Save, Add and Cancel buttons to display appropriate messages. Now, we can add an inherited form by selecting the Add Inherited Form option from the shortcut menu. The steps are:
In the Solution Explorer window, right-click and select the Windows Form. The Add New Item dialog is displayed as shown in the Fig. below. Select the Add Inherited Form option.
In the Add New Item dialog box, we specify the name of the inherited form as Inheritedform and click the Add button. The Inheritance Picker dialog box is displayed as shown in the Fig. below. Next, we specify the component from which we want to inherit the form. The component named BaseForm is selected by default. Click the OK button to accept the default setting.
After we have added an inherited form to the project, it appears as shown in the Fig. below.
Add the following code for the InheritedForm.
At runtime when these buttons are clicked, the respective messages are displayed.
Visual Inheritance in C#.Net
The concept of implementing visual Inheritance in C#.net is similar in Visual Basic.net. To see the explanation of visual Inheritance.
Code of BaseForm in C#
Code of InheritedForm in C#
Why to use Visual Inheritance ? To create consistent windows forms in vb.net and c#, we use visual inheritance. Visual inheritance allows us to reuse the windows form and also to extend the functionality in multiple forms.
To implement Visual Inheritance, we create a windows Form (base form ) and inherit forms from the base form. This is known as Form inheritance. The advantage of using this feature is that when changes are made to the base form, all the inherited forms will automatically adapt the changes.
A typical Accounting module of an ERP software consists of data entry forms to enter Cash Voucher data, Cheque Voucher data and Receipt data. All these data entry forms have similar user interface. We will consider a simple example to demonstrate Visual Inheritance feature. In Visual Basic 2008, we create a base form in a project and then create another form that inherits from the base form.
How to override base form methods in inherited form - Visual Inheritance The inherited form inherits all the features of the base form. In addition, it can override the base form Overridable methods declared with Protected or Public access modifiers. Since the requirement is to create a similar user interface for all the data entry forms, we create a base form that has the common controls and buttons. We can then inherit all the data entry forms from the base form. To provide a meaningful name to the form, set the form name as 'BaseForm' and the text as 'BaseForm'. The following are the some of the controls in the base form and their properties.

ControlType | ControlName | ControlText |
---|---|---|
Button | btnSave | Save |
Button | btnclose | Cancel |
Button | btnAdd | Add |
Add the following code in the BaseForm.
Public Class BaseForm Public Overridable Sub Add() EditMode(True) End Sub Public Overridable Sub EditMode(ByVal lEdit As Boolean) If lEdit = True Then btnSave.Visible = True btnCancel.Visible = True Else btnSave.Visible = False btnCancel.Visible = False End If End Sub Public Overridable Sub save() MsgBox("Base Form Save Button Clicked") End Sub Public Overridable Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Me.save() End Sub Public Overridable Sub Cancel() MsgBox("Base Form Cancel Button Clicked") Me.Close() End Sub Public Overridable Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Me.Cancel() End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click Me.Add() End Sub End Class
Creating an inherited form using Visual Inheritance After the base form is designed, we can create an inherited form. First build the application by selecting the build option. Click on Save, Add and Cancel buttons to display appropriate messages. Now, we can add an inherited form by selecting the Add Inherited Form option from the shortcut menu. The steps are:
In the Solution Explorer window, right-click and select the Windows Form. The Add New Item dialog is displayed as shown in the Fig. below. Select the Add Inherited Form option.

In the Add New Item dialog box, we specify the name of the inherited form as Inheritedform and click the Add button. The Inheritance Picker dialog box is displayed as shown in the Fig. below. Next, we specify the component from which we want to inherit the form. The component named BaseForm is selected by default. Click the OK button to accept the default setting.

After we have added an inherited form to the project, it appears as shown in the Fig. below.

Add the following code for the InheritedForm.
Public Class InheritedForm Public Overrides Sub save() MsgBox("Inherited Form Save Button Clicked") MyBase.EditMode(False) End Sub End Class
At runtime when these buttons are clicked, the respective messages are displayed.
Visual Inheritance in C#.Net
The concept of implementing visual Inheritance in C#.net is similar in Visual Basic.net. To see the explanation of visual Inheritance.
Code of BaseForm in C#
namespace FinCsharpProject { public partial class BaseForm : Form { public BaseForm() { InitializeComponent();} public virtual void Add() { EditMode(true);} public virtual void EditMode(bool lEdit) { if (lEdit == true) { btnSave.Visible = true; btnCancel.Visible = true;} else { btnSave.Visible = false; btnCancel.Visible = false;} } public virtual void save() { MessageBox.Show("Base Form Save Button Clicked");} public virtual void cancel() { MessageBox.Show("Base Form Save Button Clicked"); this.Close();} public virtual void btnAdd_Click(object sender, EventArgs e) { this.Add(); } public virtual void btnCancel_Click(object sender, EventArgs e) { this.cancel(); } public virtual void btnSave_Click(object sender, EventArgs e) { this.save(); } } }
Code of InheritedForm in C#
namespace FinCsharpProject { public partial class InheritedForm : FinCsharpProject.BaseForm { public InheritedForm() { InitializeComponent(); } public override void save() { MessageBox.Show("Inherited Form Save Button Clicked"); base.EditMode(false); } } }
Object Oriented Programming Articles
- Abstract class constructors
- abstract class inheritance in c#.net
- abstract class inheritance in visual basic.net
- New C# Language Feature: Automatic Properties
- Calling base class constructor in C#
- base keyword in c#
- Compile time polymorphism - Method OverLoading
- How to initialize Base class Object from inside Derived Class constructor
- Constructor overloading in vb.net
- How to use inheritance in c#
- How to use inheritance in visual basic.net
- Parameter array in vb.net
- Procedure OverLoading in vb.net
- Visual Inheritance with Windows Forms
- Azure Platform
- GridView CommandField example
- GridView and DetailsView Master/Detail page using SqlDataSource control
- POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
- Query entity data model using linq to entities
- Difference between arraylist and list collection
- How to create a Web service using Visual Studio.net
- FormView DataBound Event
- Calling base class constructor in C#
- Convert a sequence to a generic list using ToList()method
- Project ideas for students
- Accounting Software
- Creating an ASP.Net MVC 3 application
- Using assemblies in .net applications
- How to implement form validation using ASP.Net 2.0 Validation Controls
- Constructors in Visual Basic.Net