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 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.

data entry form demonstrates Visual Inheritance

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()
End Sub

Public Overridable Sub EditMode(ByVal lEdit As Boolean)
	If lEdit = True Then
		btnSave.Visible = True
		btnCancel.Visible = True
		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
End Sub

Public Overridable Sub Cancel()
	MsgBox("Base Form Cancel Button Clicked")
End Sub
Public Overridable Sub btnCancel_Click(ByVal sender As System.Object, 
			ByVal e As System.EventArgs) Handles btnCancel.Click
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, 
			ByVal e As System.EventArgs) Handles btnAdd.Click
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.

The Add New Item dialog box

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.

The Inheritance Picker dialog box

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

inherited form

Add the following code for the InheritedForm.

Public Class InheritedForm
Public Overrides Sub save()
MsgBox("Inherited Form Save Button Clicked")
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 is similar in Visual 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;}
			{	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");
	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)
	{;	}

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");