Windows Forms: Using Forms Inheritance
This sample shows how you can use inheritance with Windows Forms. A Form is
really only a class, like any other class in .NET, except that it has the
capability to display visual user interface elements. To use this type of
inheritance simply create a class that derives from a class that, in turn,
derives from System.Windows.Forms.Form.
Featured Highlights:
-
This application uses frmMain to launch two other forms that derive from
frmBase.vb: frmDataGrid and frmRichTextBox. Each of these two forms inherit the
frmBase class which includes a Label, a Button, other Form properties that
affect its appearance, and a function that returns a DataSet. The derived forms
fill in the empty middle area of frmBase with either a DataGrid or a
RichTextBox, displaying the same DataSet.
-
ADO.NET is used to retrieve the information from SQL Server. The data is loaded
into a DataSet, and the DataGrid is populated by binding to the DataSet.
-
The controls on this form utilize anchoring so that the form resizes
intelligently without the developer having to write any code.
-
The DataGrid's appearance is formatted via table and column style objects.
-
Two additional Label controls, with their Text properties set to an empty
string, are also in place on frmBase. One is declared Protected and the other
Private. (Controls placed by the Visual Studio Designer are declared with the
Friend modifier by default.) On the inherited Forms try to access the members
of each Label. You will notice that only the Label declared as Protected is
accessible.
Requirements:
This application requires the release version of Visual Studio .NET Professional
(or greater) and access to the Northwind database residing in SQL Server or the
Microsoft Data Engine (MSDE). To install MSDE, do the following:
1. Open the Start menu, click Programs, click Microsoft .NET
Framework SDK, and then click Samples and Quickstart Tutorials.
2. Click "Step 1: Install the .NET Framework Samples Database".
3. Open a command window, and CD to
\Samples\Setup This is typically : C:\Program Files\Microsoft
Visual Studio .NET\FrameworkSDK\Samples\Setup
4. Type: osql -E -S (local)\NetSDK -i InstNwnd.sql
Running the Sample:
To view the Forms that derive from frmBase (i.e., that use inheritance) in the
Designer, you will first have to compile the project. If you open a derived
Form (frmRichTextBox or frmDataGrid) before compiling, close the Form, compile,
and then reopen the Form.
See also
Advanced Basics: Using Inheritance in Windows Forms Applications