|
Visual Inheritance with Visual C# |
Let's assume you already know what Inheritance in Object-Oriented Programming is. That is, class and interface inheritance which provides the luxury of code reusability. Now it is very nice that the same feature that applies to classes and their methods also is now available the GUI forms that we create, this means that you can create a base form with your corporate logo so that it appears on all of the firm's screens. This mis called Visual Inheritance!
Let's create the Base form from which we are going to inherit from. No actual implementation of the base class, it will only be inherited from. As in creating an abstract base class, the form will be created as a Class Library. Later in this article, a form will be created that will inherit from this base form.
Creating The Base Form


This is where you right click.

Change the Output type to Class Library and click OK.
Why did we do that?
Simple, remember at the beginning of the lab we had briefed you on the approach we will be taking. That is why we will create it as a Class Library instead of a Windows Form since we won't be implementing the form by itself but will just be inheriting from it. We just changed the form Output type property to Class Library so when we compile the form it gives us a Class Library (a DLL) rather than an executable form. We will use this Class Library to inherit our all other forms.

This is how a PictureBox control looks on the ToolBox.
PictureBox
control on the form, and in the Properties window modify its Image
property. Click on the button with the ellipse on it.

This is how the property box looks.
PictureBox now. Drag and
drop the logo to the top left hand corner.

Your form should now look like the one above.
Buttons
from the ToolBox on to the Form.
Text property of the
first button to About and the next one to Help.

Your form should now look like this.
MessageBox.Show("About Box appears here!");
Why did we do that?
In C# the line that we typed in would display a message box on the user's screen displaying the message that the About box would appear in its place. Since we don't want to waste time creating a regular About box we are just inserting a "place holder" for the About box. This message would pop-up every time you click on the About button.

Click on the View Designer icon.
MessageBox.Show ("Help appears here!");
Now what exactly we've done is that we've
created two buttons and a picture box displaying our fictitious company's
logo. This would appear in all the forms that will inherit it. Now the About
button would always display information about the company. But the Help
button should display help information on the current form, right? That is,
if a login screen is inheriting from this base form, and if the user clicks
on the Help button on the login screen, it should display help info on the
login screen, and if the user clicks on the help button on some data entry
screen, it should display help for that particular screen. But we are
hard-coding on this base form. How would we achieve this? Simple, ever heard
of overriding? Simply override the Button2_Click function in
the form that's inheriting it, so that it displays the proper help! But for
this to happen, we need to follow a few steps. Starting from the one
described below.
Modifiers property to Private.
Private modifier makes sure that the inheritor cannot modify
the behavior of this button.

Modify the Modifiers properties to Private
Modifiers property to Protected. This would
allow the event handler of the Help button to be changed in the inherited
forms.
Creating the Derived Form
A tip!
In Visual Studio .NET, you can have more than a single project open at the same time. Visual Studio .NET offers us a new kind of a file type, Solution. A solution is a collection of projects. These projects could be written in any of the various different languages that are supported by .NET. So a single solution could have C#, VB.NET and JScript projects!! And also Forms, HTML, CSS, Class Libraries!!
Why did we do that?
We need to add the new project that we are creating to the existing (the base form) solution. That's the reason we selected the Add to Solution radio button. If the default Close solution was left selected, then the current solution would have closed and a new solution would have been created.

Double click on inSoftBaseForm and click OK.

Select Add Inherited Form
Form1
in the inSoftInheritedForm project. This form is derived from the
inSoftBaseForm project. Open the Form1 designer if it isn't so.There you are the form inherits its parents! Looks so much similar, doesn't it? Note the color difference among the buttons depicting the different access modifiers. Try modifying the properties, and have fun exploring!
|
Home •
Site Map •
Contact Us •
Webmaster |