Chapter 10 discussed how to create classes by inheriting from other classes. We have also used inheritance to create Forms that display a GUI, by deriving our new Form classes from class System.Windows.Forms.Form. This is an example of visual inheritance. The derived Form class contains the functionality of its Form base class, including any base-class properties, methods, variables and controls. The derived class also inherits all visual aspectssuch as sizing, component layout, spacing between GUI components, colors and fontsfrom its base class.
Visual inheritance enables you to achieve visual consistency across applications. For example, you could define a base Form that contains a product's logo, a specific background color, a predefined menu bar and other elements. You then could use the base Form throughout an application for uniformity and branding.
Class VisualInheritanceForm (Fig. 14.45) derives from Form. The output depicts the workings of the program. The GUI contains two Labels with text Bugs, Bugs, Bugs and Copyright 2006, by deitel.com., as well as one Button displaying the text Learn More. When a user presses the Learn More Button, method learnMoreButton_Click (lines 1622) is invoked. This method displays a MessageBox that provides some informative text.
Figure 14.45. Class VisualInheritanceForm, which inherits from class Form, contains a Button (Learn More).
1 // Fig. 14.45: VisualInheritanceForm.cs 2 // Base Form for use with visual inheritance. 3 using System; 4 using System.Windows.Forms;5 6 // base Form used to demonstrate visual inheritance 7 public partial class VisualInheritanceForm : Form 8 { 9 // default constructor 10 public VisualInheritanceForm() 11 { 12 InitializeComponent(); 13 } // end constructor 14 15 // display MessageBox when Button is clicked 16 private void learnMoreButton_Click( object sender, EventArgs e ) 17 { 18 MessageBox.Show( 19 "Bugs, Bugs, Bugs is a product of deitel.com", 20 "Learn More", MessageBoxButtons.OK, 21 MessageBoxIcon.Information ); 22 } // end method learnMoreButton_Click 23 } // end class VisualInheritanceForm |
To allow other Forms to inherit from VisualInheritanceForm, we must package VisualInheritanceForm as a .dll (class library). Right click the project name in the Solution Explorer and select Properties, then choose the Application tab. In the Output type drop-down list, change Windows Application to Class Library. Building the project produces the .dll.
To visually inherit from VisualInheritanceForm, first create a new Windows application. In this application, add a reference to the .dll you just created (located in the previous application's bin/Release folder). Then open the file that defines the new application's GUI and modify the first line of the class so that it inherits from class VisualInheritanceForm. Note that you will only need to specify the class name. In design view, the new application's Form should now display the controls of the base Form (Fig. 14.46). We can still add more components to the Form.
Figure 14.46. Form demonstrating visual inheritance.
(This item is displayed on page 705 in the print version)
Class VisualInheritanceTestForm (Fig. 14.47) is a derived class of VisualInheritanceForm. The output illustrates the functionality of the program. The GUI contains those components derived from class VisualInheritanceForm, as well as an additional Button with text Learn The Program. When a user presses this Button, method learnProgramButton_Click (lines 1722) is invoked. This method displays another MessageBox providing different informative text.
Figure 14.47 demonstrates that the components, their layouts and the functionality of base-class VisualInheritanceForm (Fig. 14.45) are inherited by VisualInheritanceTestForm. If a user clicks the button Learn More, the base class event handler learnMoreButton_Click displays a MessageBox. VisualInheritanceForm uses a private access modifier to declare its controls, so class VisualInheritanceTestForm cannot modify the controls inherited from class VisualInheritanceForm.
Figure 14.47. Class VisualInheritanceTestForm, which inherits from class VisualInheritanceForm, contains an additional Button.
1 // Fig. 14.47: VisualInheritanceTestForm.cs 2 // Derived Form using visual inheritance. 3 using System; 4 using System.Windows.Forms; 5 6 // derived form using visual inheritance 7 public partial class VisualInheritanceTestForm : 8 VisualInheritanceForm // code for inheritance 9 { 10 // default constructor 11 public VisualInheritanceTestForm() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // display MessageBox when Button is clicked 17 private void learnProgramButton_Click(object sender, EventArgs e) 18 { 19 MessageBox.Show( "This program was created by Deitel & Associates", 20 "Learn the Program", MessageBoxButtons.OK, 21 MessageBoxIcon.Information ); 22 } // end method learnProgramButton_Click 23 } // end class VisualInheritanceTestForm |
User Defined Controls |
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index