30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13 Graphical User Interfaces Concepts: Part 2 577<br />

<strong>Visual</strong> inheritance enables developers <strong>to</strong> achieve visual consistency across applications<br />

by reusing code. For example, a company could define a base form that contains a product’s<br />

logo, a static background color, a predefined menu bar and other elements. <strong>Program</strong>mers then<br />

could use the base form throughout an application for purposes of uniformity and branding.<br />

Class FrmInheritance (Fig. 13.39) is a derived class of class Form. The output<br />

depicts the workings of the program. The GUI contains two labels with text Bugs, Bugs,<br />

Bugs and Copyright 2002, by Bug2Bug.com., as well as one but<strong>to</strong>n displaying the<br />

text Learn More. When a user presses the Learn More but<strong>to</strong>n, method<br />

cmdLearn_Click (lines 16–22) is invoked. This method displays a message box that<br />

provides some informative text.<br />

To allow other forms <strong>to</strong> inherit from FrmInheritance, we must package FrmInheritance<br />

as a .dll. Right click the project's name in the Solution Explorer and<br />

choose Properties. Under Common Properties > General, change Output Type <strong>to</strong><br />

Class Library. Building the project produces the .dll.<br />

To visually inherit from FrmInheritance, we create an empty project. From the<br />

Project menu, select Add Inherited Form... <strong>to</strong> display the Add New Item dialog.<br />

Select Inherited Form from the Templates pane. Clicking OK displays the Inheritance<br />

Picker <strong>to</strong>ol. The Inheritance Picker <strong>to</strong>ol enables programmers <strong>to</strong> create a form<br />

which inherits from a specified form. Click but<strong>to</strong>n Browse and select the .dll file corresponding<br />

<strong>to</strong> FrmInheritance. This .dll file normally is located within the project’s<br />

bin direc<strong>to</strong>ry. Click OK. The Form Designer should now display the inherited form<br />

(Fig. 13.40). We can add components <strong>to</strong> the form.<br />

1 ' Fig. 13.39: FrmInheritance.vb<br />

2 ' Form template for use with visual inheritance.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Public Class FrmInheritance<br />

7 Inherits Form<br />

8<br />

9 Friend WithEvents lblBug As Label ' <strong>to</strong>p label<br />

10 Friend WithEvents lblCopyright As Label ' bot<strong>to</strong>m label<br />

11 Friend WithEvents cmdLearn As But<strong>to</strong>n ' left but<strong>to</strong>n<br />

12<br />

13 ' <strong>Visual</strong> Studio .<strong>NET</strong> generated code<br />

14<br />

15 ' invoked when user clicks Learn More but<strong>to</strong>n<br />

16 Private Sub cmdLearn_Click(ByVal sender As System.Object, _<br />

17 ByVal e As System.EventArgs) Handles cmdLearn.Click<br />

18<br />

19 MessageBox.Show("Bugs, Bugs, Bugs is a product of " & _<br />

20 " Bug2Bug.com.", "Learn More", MessageBoxBut<strong>to</strong>ns.OK, _<br />

21 MessageBoxIcon.Information)<br />

22 End Sub ' cmdLearn_Click<br />

23<br />

24 End Class ' FrmInheritance<br />

Fig. 13.39 Class FrmInheritance, which inherits from class Form, contains a<br />

but<strong>to</strong>n (Learn More) (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!