Visual Inheritance

Visual Inheritance

Back in this book's early chapters, we learned about object-oriented programming and how to inherit objects from prebuilt classes. We learned that each form is nothing more than a class that inherits from System.Windows.Forms.Form. Taking the concept of inheritance a step further, we can easily inherit from a base form to create identical child forms. Why is the ability to do this so great? How many times have you had to re-create the look and feel of a form for your company? Now this task becomes trivial using visual inheritance.

Let's say you have a form that your company wants to use to present a common look and feel throughout its applications. You could develop a base form that you can inherit from and make any specific changes on child forms that inherit from the base. In a base company form, we might have an About button that's private and can't be overridden in a child form. This way we can have a consistent About statement on all forms. We could also provide a link to our Web site that can't be overridden in a child form. On the other hand, it might be helpful to inherit the Help button in child forms so that context-sensitive help can be provided on each child form. Our base form might look something like Figure 14-1.

Figure 14-1

Our base form.

Building a Base Form

Here are the steps we'll take to build our base form and inherit from it.

  1. Create a Windows project and name it CompanyBase. Add an image that represents a company logo, two buttons as shown in Figure 14-1, and a LinkLabel control that will point to your company's Web site. Set the control properties as shown in Table 14-1.

    Table 14-1  Properties for the CompanyBase Form and Controls

    Object

    Property

    Value

    Form

    Name

    CompanyForm

    Text

    Solid State Software

    PictureBox

    Name

    pbLogo

    Button

    Name

    btnAbout

    Text

    &About

    Button

    Name

    btnHelp

    Text

    &Help

    LinkLabel

    Name

    llWebSite

    Text

    Visit our Web Site

  2. Add a message in the btnAbout button's Click event. The signature for this event is private, so the message can't be overridden in any child form. When the user clicks the About button, code in the base form will execute.

    Private Sub btnAbout_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles BbtnAbout.Click Dim sMessage As String = _ "Developing .NET Solutions since 2001" MessageBox.Show(sMessage, "Solid State Software, Inc.", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub

  3. Change the signature of the Click event procedure for the btnHelp button to be Protected Overridable. This procedure can be overridden by an identically named procedure in a derived class.

    Protected Overridable Sub btnHelp_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnHelp.Click End Sub

  4. In order to inherit from our base form, we have to change the output type from a Windows Application to a Class Library. Right-click the CompanyBase project, and select Properties. Change the Output Type setting to Class Library and the Startup Object setting to None. Now our form is a class file that can be inherited from. Notice in Figure 14-2 that the form will be compiled as CompanyBase.dll.

    Figure 14-2

    Change the output type to Class Library.

  5. Before we can inherit from our form, we have to build it. Select Build | Build Solution to compile our new class. You can only inherit forms if they are compiled into a DLL.

Adding the Inherited Form

In the next set of steps, we'll add an inherited form to the project so that you can see visual inheritance at work.

  1. Bring up the Solution Explorer and click CompanyBase. Right-click and select Add | Add Inherited Form. The Add New Item dialog box, shown in Figure 14-3, will be displayed. Select Inherited Form, and then rename the form to ChildForm.vb.

    Figure 14-3

    Add an inherited form named ChildForm.vb.

  2. Click Open, and the Inheritance Picker dialog box will be displayed, shown in Figure 14-4. Select the form you want to inherit from, CompanyForm in this case, and click OK. If you take a look at the Solution Explorer, you can see that we now have two projects in our solution.

    Figure 14-4

    Select CompanyForm in the Inheritance Picker dialog box.

    Now look closely at the inherited ChildForm in the IDE. Notice that each of the controls that have been inherited is displayed with an arrow in a white box, as you can see in Figure 14-5. This cue tells us that these controls are inherited from the base form.

    Figure 14-5

    Inherited controls on the form are distinguished by an arrow in a white box.

  3. Double-click the child form to bring up the code window. You'll see in the code that ChildForm inherits from CompanyBase.CompanyForm.

    Public Class ChildForm Inherits CompanyBase.CompanyForm

  4. We want to provide context-sensitive help that is specific to each child form. We can now easily do this by overriding the Click event of the Help button of the parent form. The signature of the event in each child form must be identical to the base form's, so you might want to simply cut and paste the subroutine from the parent to the child. Modify the signature to add Protected Overrides before the Sub.

    Protected Overrides Sub btnHelp_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnHelp.Click Dim sMessage As String = "Context Sensitive Help" MessageBox.Show(sMessage, "Child Form", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub

  5. Add a few controls to the child form. You can use these controls for code that performs operations such as getting a login ID and password, getting employee information, displaying database information, and so on. A modified child form is shown in Figure 14-6.

    Figure 14-6

    Add some controls to the child form.

  6. The last step we have to take is to indicate in the project which form should be used as the startup form. We can't run the base form because it is now a class library, so we will select the child form. Right-click CompanyBase in the Solution Explorer, and select Properties. Select Windows Application as the output type and ChildForm as the startup object.

  7. Press F5 to start the program. Click the About button. The code in the base form is executed. Now click the Help button, and the overridden Click event in the child form is executed. You can easily see how you can neatly package code that is common to all forms in the base and then use child-specific code in each inherited child form.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net