Tuesday, May 25, 2004
Visual Inheritance in VB.Net
Inheritance
Before getting into the Visual Inheritance, we should know Inheritance and how it is done in VB.Net.
Inheritance is the concept that a new class can be based on an existing class, inheriting the interface and functionality from the original class.
Inheritance, for instance, is also sometimes referred to as generalization. This is because the class from which we are inheriting our behavior is virtually always a more general from of our new class.
Consider the following example,
We have a class called Vehicle which contains properties like no of wheels, how many people can sit.
Now consider the class Bi-Cyle, Motor Cycle, Car etc., Which all have the above mentioned properties, many other common properties and some properties on their own (for eg., Car will have the type of fuel property). All these classes are basically Vehicles.
Inheritance makes it very easy to create classes Bi-cyle, Motor Cycle and Car and so forth. We dont have to redefine those properties for a Car to become a vehicle, it automatically gets any properties, methods and events from the original Person class.
The class from which the properties and methods are inherited is called a base class. It is also often referred to as a super class or a parent class. And the class which inherits the base class is called a Derived class or a sub class. It is also referred as the derived class.
is-a Relation ship
Inheritance relationship is also referred to as an is-a relationship. When we create a Car class that inherits from a vehicle class, Then Car class is a vehicle.
Forms
Now, just for sometime we will move to the VB6.0 where many of us would have worked on the form (VB6.0 Form), If we pay more attention and examine it carefully, we can realize that both a form and a class are one and the same thing, when compared to classes forms have added feature of a user interface. VB6.0 has done a wonderfully job of hiding it.
Not only that, we design our form in VB6.0 by putting some Command Buttons, Text boxes and what not. Have you ever taken some time and thought where am I declaring the controls in the code ? You magically get a variable defined and instantiated for you. VB6.0 writes the code and hides it from the user.
If you are really interested in knowing what is happening behind the scenes in VB6.0, just the do the following steps carefully, you yourself will find all those magics done carefully in VB6.0, If you are not really not interested just proceed with the remaining part of the document
Create a new VB6.0 Project
By default on form will be added
Design your form and write some code(for eg., put two text boxes and place of command button, in the command button click event the write the code to display as message box displaying the sum of two numbers entered in the textboxes)
Now save the project
You will be getting three files one is your project, Source safe file and other one is the form file
Now open the .frm file in any of the text editors, you can see the entire code written in that file
You can see the entire code cleanly written and kept hidden from you when viewed in the VS IDE.
You can see your Textboxes, command button and form properties defined there. Not only that the code which you have written.
In Vb.net, all the forms are now true classes, and its abundantly obvious. There are no more secrets going on behind the scenes. All the properties are defined directly in code.
In Vb6.0, forms were saved as .frm files, But in VB.Net the forms are classes, they are stored in the extension .VB just like conter classes.
By inheriting from System.Windows.Froms.Form, any class automatically gets all the properties methods, and events that a form based on Windows forms is supposed to have.
Note:
Create a new form in VB.Net, get into the coding window you can see the declaration of the class like this.
Public class formName inherits System.Windows.Forms.Form
Visual Inheritance
Now, we have discussed about the Inheritance. We will see how this inheritance can be used to help us.
.Net fully utlises the OOPs concept fully.
As we discussed earlier, a form in .Net is nothing but a class that inherits System.Windows.Forms.Form class. We know that Inheritance defines a is a relationship between the derived and super class.
So class does not have to inherit directly from the system.window.forms.form class inorder to become a form. It can become a form by inheriting from another form, which itself inherits from System.Windows.Forms.Form. By doing so, controls originally placed on one form can be directly inherited by a secord form. Not only the design of the original form inherited, but also any code associated with these controls. This method of creating a form is called as the Visual Inheritance.
It can be done in Two ways.
1. Create a class and inheriting the form
Create a Form from which the other form is to be inherited.
Add a new class and inherit the form using the inherits keyword
2. Using the Inheritance Picker.
Design the form.
Right Click on the project and choose Add Inherited Form.
Which open the Inheritance Picker from which the user can pick the form from which it has to inherit
Note that the from must be compiled into either an .EXE or a DLL, Before it can be used by the Inheritance Picker.
Advantages of Visual Inheritance
Visual Inheritance provides lot of advantages
Code Reusability
Defining the UI once and using it in the other forms
Modifying the UI is very simple
Doesnt require the Code to be re-compiled once again when the UI is changed
Example
Consider a application where we want all the forms in the project to have a logo on the top left corner and a label displaying a company name and Date & Time running in the right side of the form.
Without Visual Inheritance we need to design and write code in all the form inorder to get the following functionality.
To avoid that we will be create a form with all these functionalities and extends that form to all other classes.
Step 1: Defining the Template Form
Open a New Windows Application Project. By default one from will be there rename the form to TemplateForm
Add the following controls, add one picture box, Three Labels and name them us lblcompanyname, lbldate and lbltime
And one timer, set the interval property to 1000 and set enable property as true
Add the following code in the Form Load Event (Code to display the company name in the lable)
LblCompanyName.Text = Me.Text
Add the following code in the Timer Tick event
lbltime.Text = Format(Now, "hh:mm:ss tt")
lblDate.Text = Format(Now, "dd/MM/yyyy")
Build the project
Step 2: Using the Visual Inheritance
Right Click on the project and select ADD ADD Inherited Form
Type the form name in the Add new Item window and click open
Inheritance Picker window will appear as shown below
If the template form is in the current project, slected the form form the list provided and click ok
Or else click browse to selected the dll containing the form. On selected the dll, the window will display all the classes available in the dll
Select the form and click ok
On Clicking ok we will be getting a form with the same design we did for the template form
But we can find a small difference, in the all the controls that belong to the parent class contains an arrow mark at the top, denoting that they are inherited components
Note that all the properties of the components are readonly, we cant change any property, we will see how to over come that in our proceeding discussion.
Now, set the start up form by clicking on the project and selecting the start up form.
Changet the Form Text Property.
And run the form
You could see the timer running and the Company name changed.
Accessing the Controls from the Inherited form
As we all know about the scope of a object. So by changing the scope of the controls in the Template form we can acces the controls in the Inherited form also.
The following table lists all the available scope options:
Scope Description
Public Available only to code within our class
Protected Avaiable only to classes that inherit from our class
Firend Available only to code within out project/component
Protected Frient Available to classes that inherit from our class and to code within our project/component. This is a combination of Protected and Friend
Public Avaiable to code outside our class
Changing the UI of the Base Form
Changing the UI of the form is very simple.
If the Base Form is in the same project then changing the form and compiling the form is enough. It make all the changes in the other forms.
If the base form is derived from the other forms cantained in a dll, then make the changes in the master from and build the dll. And replace that dll with the old one.
Before getting into the Visual Inheritance, we should know Inheritance and how it is done in VB.Net.
Inheritance is the concept that a new class can be based on an existing class, inheriting the interface and functionality from the original class.
Inheritance, for instance, is also sometimes referred to as generalization. This is because the class from which we are inheriting our behavior is virtually always a more general from of our new class.
Consider the following example,
We have a class called Vehicle which contains properties like no of wheels, how many people can sit.
Now consider the class Bi-Cyle, Motor Cycle, Car etc., Which all have the above mentioned properties, many other common properties and some properties on their own (for eg., Car will have the type of fuel property). All these classes are basically Vehicles.
Inheritance makes it very easy to create classes Bi-cyle, Motor Cycle and Car and so forth. We dont have to redefine those properties for a Car to become a vehicle, it automatically gets any properties, methods and events from the original Person class.
The class from which the properties and methods are inherited is called a base class. It is also often referred to as a super class or a parent class. And the class which inherits the base class is called a Derived class or a sub class. It is also referred as the derived class.
is-a Relation ship
Inheritance relationship is also referred to as an is-a relationship. When we create a Car class that inherits from a vehicle class, Then Car class is a vehicle.
Forms
Now, just for sometime we will move to the VB6.0 where many of us would have worked on the form (VB6.0 Form), If we pay more attention and examine it carefully, we can realize that both a form and a class are one and the same thing, when compared to classes forms have added feature of a user interface. VB6.0 has done a wonderfully job of hiding it.
Not only that, we design our form in VB6.0 by putting some Command Buttons, Text boxes and what not. Have you ever taken some time and thought where am I declaring the controls in the code ? You magically get a variable defined and instantiated for you. VB6.0 writes the code and hides it from the user.
If you are really interested in knowing what is happening behind the scenes in VB6.0, just the do the following steps carefully, you yourself will find all those magics done carefully in VB6.0, If you are not really not interested just proceed with the remaining part of the document
Create a new VB6.0 Project
By default on form will be added
Design your form and write some code(for eg., put two text boxes and place of command button, in the command button click event the write the code to display as message box displaying the sum of two numbers entered in the textboxes)
Now save the project
You will be getting three files one is your project, Source safe file and other one is the form file
Now open the .frm file in any of the text editors, you can see the entire code written in that file
You can see the entire code cleanly written and kept hidden from you when viewed in the VS IDE.
You can see your Textboxes, command button and form properties defined there. Not only that the code which you have written.
In Vb.net, all the forms are now true classes, and its abundantly obvious. There are no more secrets going on behind the scenes. All the properties are defined directly in code.
In Vb6.0, forms were saved as .frm files, But in VB.Net the forms are classes, they are stored in the extension .VB just like conter classes.
By inheriting from System.Windows.Froms.Form, any class automatically gets all the properties methods, and events that a form based on Windows forms is supposed to have.
Note:
Create a new form in VB.Net, get into the coding window you can see the declaration of the class like this.
Public class formName inherits System.Windows.Forms.Form
Visual Inheritance
Now, we have discussed about the Inheritance. We will see how this inheritance can be used to help us.
.Net fully utlises the OOPs concept fully.
As we discussed earlier, a form in .Net is nothing but a class that inherits System.Windows.Forms.Form class. We know that Inheritance defines a is a relationship between the derived and super class.
So class does not have to inherit directly from the system.window.forms.form class inorder to become a form. It can become a form by inheriting from another form, which itself inherits from System.Windows.Forms.Form. By doing so, controls originally placed on one form can be directly inherited by a secord form. Not only the design of the original form inherited, but also any code associated with these controls. This method of creating a form is called as the Visual Inheritance.
It can be done in Two ways.
1. Create a class and inheriting the form
Create a Form from which the other form is to be inherited.
Add a new class and inherit the form using the inherits keyword
2. Using the Inheritance Picker.
Design the form.
Right Click on the project and choose Add Inherited Form.
Which open the Inheritance Picker from which the user can pick the form from which it has to inherit
Note that the from must be compiled into either an .EXE or a DLL, Before it can be used by the Inheritance Picker.
Advantages of Visual Inheritance
Visual Inheritance provides lot of advantages
Code Reusability
Defining the UI once and using it in the other forms
Modifying the UI is very simple
Doesnt require the Code to be re-compiled once again when the UI is changed
Example
Consider a application where we want all the forms in the project to have a logo on the top left corner and a label displaying a company name and Date & Time running in the right side of the form.
Without Visual Inheritance we need to design and write code in all the form inorder to get the following functionality.
To avoid that we will be create a form with all these functionalities and extends that form to all other classes.
Step 1: Defining the Template Form
Open a New Windows Application Project. By default one from will be there rename the form to TemplateForm
Add the following controls, add one picture box, Three Labels and name them us lblcompanyname, lbldate and lbltime
And one timer, set the interval property to 1000 and set enable property as true
Add the following code in the Form Load Event (Code to display the company name in the lable)
LblCompanyName.Text = Me.Text
Add the following code in the Timer Tick event
lbltime.Text = Format(Now, "hh:mm:ss tt")
lblDate.Text = Format(Now, "dd/MM/yyyy")
Build the project
Step 2: Using the Visual Inheritance
Right Click on the project and select ADD ADD Inherited Form
Type the form name in the Add new Item window and click open
Inheritance Picker window will appear as shown below
If the template form is in the current project, slected the form form the list provided and click ok
Or else click browse to selected the dll containing the form. On selected the dll, the window will display all the classes available in the dll
Select the form and click ok
On Clicking ok we will be getting a form with the same design we did for the template form
But we can find a small difference, in the all the controls that belong to the parent class contains an arrow mark at the top, denoting that they are inherited components
Note that all the properties of the components are readonly, we cant change any property, we will see how to over come that in our proceeding discussion.
Now, set the start up form by clicking on the project and selecting the start up form.
Changet the Form Text Property.
And run the form
You could see the timer running and the Company name changed.
Accessing the Controls from the Inherited form
As we all know about the scope of a object. So by changing the scope of the controls in the Template form we can acces the controls in the Inherited form also.
The following table lists all the available scope options:
Scope Description
Public Available only to code within our class
Protected Avaiable only to classes that inherit from our class
Firend Available only to code within out project/component
Protected Frient Available to classes that inherit from our class and to code within our project/component. This is a combination of Protected and Friend
Public Avaiable to code outside our class
Changing the UI of the Base Form
Changing the UI of the form is very simple.
If the Base Form is in the same project then changing the form and compiling the form is enough. It make all the changes in the other forms.
If the base form is derived from the other forms cantained in a dll, then make the changes in the master from and build the dll. And replace that dll with the old one.