Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

Visual Inheritance with Visual C#

Rate me:
Please Sign up or sign in to vote.
4.13/5 (8 votes)
31 May 20018 min read 187K   37   23
Visual Inheritance allows you to inherit visual elements in your derived GUI classes.

Introduction

Let's try out one of Visual C#'s most interesting feature, Visual Inheritance. What exactly is it? Now, we do know what is inheritance, right? No, I don't mean the money that you get from your parents/grand parents, I'm talking about class and interface inheritance which saves you the headache of re-typing code and provides the luxury of code reusability. Now wouldn't it be nice if the same feature that applies to classes and their methods could also be applied to the GUI forms that we create, I mean create a base form with our corporate logo so that it appears on all of the firm's screens? Well, such a feature now exists! Yes, you've guessed it, its Visual Inheritance!

Now, let's go ahead and try it out, shall we? For our ease of understanding, we have divided this lab into two parts. In part 1, you create the Base form from which we are going to inherit from. Since we won't be actually implementing it in our project but only be inheriting from it (something like an abstract base class) we will be creating the form as a Class Library. In part 2, we will create a form that will inherit from the base form we created in part 1.

Part 1

  1. Start Visual Studio .NET by clicking on Start -> Programs -> Microsoft Visual Studio .NET 7.0 -> Microsoft Visual Studio .NET 7.0.
  2. Click File -> New -> Project.

    Image 1

  3. Select Visual C# Projects. In the right-side pane, select Windows Application, type in the name of the application as 'inSoftBaseForm' and click OK. This would bring up the WinForm IDE (taking too long? Suggestion: go get a cup of coffee!!).
  4. Right click on project name "inSoftBaseForm" in the Solution Explorer and select Properties.

    Image 2

    This is where you right click.

  5. After you've selected Properties from the context menu that appeared when you right clicked as shown above, a dialog box should appear (like the one shown below). Change the Output type to Class library and click OK. It would take you back to the Form Designer (Win Forms IDE).

    Image 3

    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.

  6. When back to the Win Form IDE, drag and drop a picture box from the toolbox on to the form.

    Image 4

    This is how a PictureBox control looks on the ToolBox.

  7. Next select the PictureBox control on the form, and in the Properties window modify its Image property. Click on the button with the ellipse on it.

    Image 5

    This is how the property box looks.

  8. A standard Windows 'Open' dialog box pops up. Go somewhere and select an image file and click Open. The image (inSoft Logo in my case) should appear in the PictureBox now. Drag and drop the logo to the top left hand corner.

    Image 6

    Your form should now look like the one above.

  9. Next drag and drop a couple of Buttons from the ToolBox on to the Form.
  10. Change the Text property of the first button to About and the next one to Help.

    Image 7

    Your form should now look like this.

  11. Double click the About button. It should open up the Code Window for you. Type in this line of code.
    C#
    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.

  12. Switch back to the Form Designer by clicking on the View Designer icon on the Solution Explorer.

    Image 8

    Click on the View Designer icon.

  13. Double click on the Help button. To enter the code window again, type in the following line of code:
    C#
    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.

  14. Switch back to the Design View by clicking on the View Designer icon on the Solution Explorer.
  15. Click once on the About button. In the Properties window, set its Modifiers property to Private. Private modifier makes sure that the inheritor cannot modify the behavior of this button.

    Image 9

    Modify the Modifiers properties to Private

  16. Now click once on the Help button and change its Modifiers property to Protected. This would allow the event handler of the Help button to be changed in the inherited forms.
  17. Phew, finally we are done! That was difficult, wasn't it? But at least it's over. Now we've got to build the Class Library, we do that by clicking on Build and then selecting Build again from the menu. This should build a DLL file - a Class Library with the form that we just created. And believe me, this is the end of part 1.

Part 2

Let's begin the sequel to our Visual Inheritance Lab. Welcome to part 2. As also said earlier, in this part we will create another form which will inherit from the base form we created in the previous part. Sounds interesting, doesn't it? Don't worry, it ain't a lot of work, let's get started.

  1. Leaving the current project open, click on File -> New -> Project to open another project in the same solution.

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

  2. You will be presented with the familiar New Project dialog box. You'll have to select Visual C# Projects on the left pane and Windows Application on the right. Select the Add to Solution radio button. Type in the name as inSoftInheritedForm and click OK.

    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.

  3. Note that the Solution Explorer lists out two projects now. Select the inSoftInheritdForm's Form1.cs. Right click and select Delete. Click OK if prompted whether to delete the form permanently.
  4. To add a reference to the Base Forms Class Library, click on Project -> Add Reference. You will be presented with an Add Reference dialog box. Switch over to the Projects tab. Double click on inSoftBaseForm. Click OK to add the reference.

    Image 10

    Double click on inSoftBaseForm and click OK.

  5. Right click on inSoftInheritedForm in the Solution Explorer. Select Add -> Add Inherited Form.

    Image 11

    Select Add Inherited Form

  6. You will be presented with an Add New Item dialog box. Verify that the inherited form item is selected and click Open.
  7. You will be presented with an inheritance picker dialog box. Over here select inSoftBaseForm and click OK.
  8. This should create a new 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!

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey5-Mar-12 19:06
professionalManoj Kumar Choubey5-Mar-12 19:06 
GeneralI miss the point of visual inheritance Pin
philippe dykmans2-Oct-09 9:51
philippe dykmans2-Oct-09 9:51 
QuestionVS2008? Pin
jon-8024-Jan-09 1:35
professionaljon-8024-Jan-09 1:35 
QuestionDesign Time Error Pin
SengulARICI30-Oct-08 1:06
SengulARICI30-Oct-08 1:06 
GeneralGreat Form Inheritance tut Pin
asmtony29-Dec-05 14:08
asmtony29-Dec-05 14:08 
QuestionWhat about inheriting non-visual components? Pin
krwitukiewicz10-Aug-05 22:23
krwitukiewicz10-Aug-05 22:23 
QuestionWhat about using Visual Inheritance through code libraries ? Pin
Braulio Dez6-Mar-03 23:41
Braulio Dez6-Mar-03 23:41 
AnswerRe: What about using Visual Inheritance through code libraries ? Pin
Ryan Farley28-Mar-03 4:50
Ryan Farley28-Mar-03 4:50 
Generalstep 16 is missing the code to type in Pin
11-Jan-02 8:27
suss11-Jan-02 8:27 
QuestionWhy not just use Delphi ? Pin
1-Jun-01 7:56
suss1-Jun-01 7:56 
AnswerRe: Why not just use Delphi ? Pin
26-Jun-01 6:12
suss26-Jun-01 6:12 
GeneralRe: Why not just use Delphi ? Pin
26-Jun-01 10:29
suss26-Jun-01 10:29 
GeneralRe: Why not just use Delphi ? Pin
26-Jun-01 23:02
suss26-Jun-01 23:02 
GeneralRe: Why not just use Delphi ? Pin
27-Jun-01 6:02
suss27-Jun-01 6:02 
GeneralRe: Why not just use Delphi ? Pin
9-Mar-02 14:52
suss9-Mar-02 14:52 
GeneralRe: Why not just use Delphi ? Pin
Senior Architect13-Nov-02 14:38
sussSenior Architect13-Nov-02 14:38 
GeneralRe: Why not just use Delphi ? Pin
Anonymous6-Jul-05 10:27
Anonymous6-Jul-05 10:27 
AnswerRe: Why not just use Delphi ? Pin
29-Oct-01 11:35
suss29-Oct-01 11:35 
AnswerRe: Why not just use Delphi ? Pin
edunphy13-Feb-02 9:15
edunphy13-Feb-02 9:15 
AnswerRe: Why not just use COBOL ??? XDDDDDDDDDD Pin
Braulio Dez6-Mar-03 23:39
Braulio Dez6-Mar-03 23:39 
COBOL, FORTRAN, ... or better.. the fastest and the best one Assembly... Smile | :)


It's a pity that borland has not the power that it had years ago, I mean Turbo Pascal, was quite good... but now... M$ has the power

AnswerRe: Why not just use Delphi ? Pin
Anonymous11-Feb-04 7:54
Anonymous11-Feb-04 7:54 
AnswerRe: Why not just use Delphi ? Pin
Anonymous17-Nov-04 15:25
Anonymous17-Nov-04 15:25 
GeneralRe: Why not just use Delphi ? Pin
angelok13-Nov-05 16:03
angelok13-Nov-05 16:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.