Export (0) Print
Expand All

Walkthrough: Demonstrating Visual Inheritance

Visual Studio .NET 2003

Visual inheritance allows you to see the controls on the base form and to add new controls. In this walkthrough you will create a base form and compile it into a class library. You will import this class library into another project and create a new form that inherits from the base form. During this walkthrough, you will learn how to:

  • Create a class library project containing a base form.
  • Add a button with properties that derived classes of the base form can modify.
  • Add a button that cannot be modified by inheritors of the base form.
  • Create a project containing a form that inherits from BaseForm.

Ultimately, the walkthrough will demonstrate the difference between private and protected controls on an inherited form

Scenario Steps

The first step is to create the base form.

To create a class library project containing a base form

  1. From the File menu, choose New and then Project to open the New Project dialog box.
  2. Create a Windows Application named BaseFormLibrary. For details, see Creating a Windows Application Project.
  3. To create a class library instead of a standard Windows application, right-click the BaseFormLibrary project node in Solution Explorer and select Properties.
  4. In the properties for the project, change the output type from Windows Application to Class Library and click OK.
  5. From the File menu, choose Save All to save the project and files to the default location.

The next two procedures add buttons to the base form. To demonstrate visual inheritance, you will give the buttons different access levels by setting their Modifiers properties.

To add a button that inheritors of the base form can modify

  1. On the Windows Forms tab of the Toolbox, double-click Button to add a button to the form. Use the mouse to position and resize the button.
  2. In the Properties window, set the following properties of the button:
    • Set the Text property to Say Hello.
    • Set the (Name) property to btnProtected.
    • Set the Modifiers property to Protected. This makes it possible for forms that inherit from Form1 to modify the properties of btnProtected.
  3. Double-click the Say Hello button to add an event handler for the Click event.
  4. Add the following line of code to the event handler:
    ' Visual Basic
    MessageBox.Show("Hello, World!")
    
    // C#
    MessageBox.Show("Hello, World!");
    

To add a button that cannot be modified by inheritors of the base form

  1. Switch to design view by clicking the Form1.vb [Design] tab above the Code Editor, or by pressing SHIFT+F7.
  2. Add a second button and set its properties as follows:
    • Set the Text property to Say Goodbye.
    • Set the (Name) property to btnPrivate.
    • Set the Modifiers property to Private. This makes it impossible for forms that inherit from Form1 to modify the properties of btnPrivate.
  3. Double-click the Say Goodbye button to add an event handler for the Click event. Place the following line of code in the event procedure:
    ' Visual Basic
    MessageBox.Show ("Goodbye!")
    
    // C#
    MessageBox.Show ("Goodbye!");
    
  4. From the Build menu, choose Build to build the class library.

    Once the library is built, you can create a new project that inherits from the form you just created.

To create a project containing a form that inherits from the base form

  1. From the File menu, choose Add Project and then New Project to open the New Project dialog box.
  2. Create a Windows application named InheritanceTest. For details, see Creating a Windows Application Project.

To add an inherited form

  1. Right-click the InheritanceTest project and select Add and then Inherited Form.
  2. In the Add New Item dialog box, verify that Inherited Form is selected, and click OK.
  3. In the Inheritance Picker dialog box, select Form1 from the BaseFormLibrary project as the form to inherit from and click OK.

    This creates a form in the InheritanceTest project that derives from the form in BaseFormLibrary.

  4. Open the inherited form in the Windows Forms Designer by double-clicking it, if it is not already open.

    In the Windows Forms Designer, the inherited buttons have a glyph (Aa984465.vbinheritanceglyph(en-us,VS.71).gif) in their upper corner, indicating they are inherited.

  5. Select the Say Hello button and observe the resize handles. Because this button is protected, the inheritors can move it, resize it, change its caption, and make other modifications.
  6. Select the private Say Goodbye button, and notice that it does not have resize handles. Additionally, in the Properties window, the properties of this button are grayed to indicate they cannot be modified. Finally, move the mouse pointer over the button, and a ToolTip will appear indicating how the control was inherited.
  7. If you are using Visual C#:
    1. Right-click Form1 in the InheritanceTest project and choose the Delete command on the shortcut menu.
    2. Add the following code to Form2, right before the line that reads protected override void Dispose( bool disposing ). This addition allows Form2 to function as a startup object.
      // C#
      static void Main ()
      {
         Application.Run (new Form2());
      }
      
  8. Right-click the InheritanceTest project in Solution Explorer and select Set As Startup Project.
  9. Right-click the InheritanceTest project in Solution Explorer and select Properties. In the InheritanceTest Property Page dialog box, set the Startup Object to be the inherited form (most likely Form2).
  10. Press F5 to run the application, and observe the behavior of the inherited form.

Further Exploration

Inheritance for user controls works in much the same way. Open a new class library project and add a user control. Place constituent controls on it and compile the project. Open another new class library project and add a reference to the compiled class library. Also, try adding an inherited control (through the Add New Items dialog box) to the project and using the Inheritance Picker. Add a user control, and change the Inherits (: in C#) statement. For more information, see Inheriting Windows Forms.

See Also

Inheriting Windows Forms | Windows Forms Inheritance | Windows Forms Walkthroughs | Windows Forms

Show:
© 2014 Microsoft