With visual inheritance, you can see the controls on the base form and add a new control. In this exercise, a base set body will be created and compiled into a class library. Then introduce this class into another project and create a new form inherited from the base form. During the drill, you will learn the operations as follows:
Create a class library project that contains the base form. Add a button with a base product derived class. Adding a button that cannot be modified by the successor of the base form. Create a project that contains the form inherited from baseform.
Finally, this exercise will demonstrate the difference between the inheritance of the private and protected control on the form.
Program step
The first step is to create a base form.
Create a library item containing the base form
Select New and Projects in order from the File menu to open the New Project dialog. Create a Windows application called BaseFormlibrary. For more information, see Creating a Windows Application Project. To create class libraries (instead of standard Windows applications), right-click the "BaseformLibrary" project node in Solution Explorer and select Properties. In the properties of the project, change the output type from the "Windows Application" to "Class Library", and then click OK. Select All Save from the File menu to save items and files to the default location.
The subsequent two processes will add the button to the base form. To demonstrate visual inheritance, grant them different access levels by setting the MODIFIERS attribute of the setting button.
Add the successor of the base form to modify the button
Double-click "Button" on the Windows Forms tab of Toolbox, add a button to the form. Use the mouse to locate the button and adjust its size. In the Properties window, set the following properties of the button:
Set the Text property to Say Hello. Set the (Name) property to btnprotaced. Set the Modifiers property to protected. In this way, the form that is inherited from the Form1 can modify the properties of btnProtaced. Double-click the SAY Hello button to add an event handler for the Click event. Add the following code line to the event handler: 'Visual Basic
Messagebox.show ("Hello, World!")
// C #
Messagebox.show ("Hello, World!");
Add buttons that cannot be modified by the successor of the base form
Click the "Form1.vb [Design] tab above the code editor or press SHIFT F7 to switch to the design view. 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. In this way, the form inherited from the Form1 cannot modify the properties of BTnPrivate. Double-click the Say Goodbye button to add an event handler for the Click event. Put the following code line into the event process: 'Visual Basic
Messagebox.show ("Goodbye!")
// C #
Messagebox.show ("Goodbye!"); Select "Generate Solution" from the Generation menu to generate class libraries. Once you are generated, you can create a project that inherits from the form you just created.
Create a project that contains the form inherited from the base form
Select "Add Project" and "New Project" from the File menu to open the New Project dialog. Create a Windows application called inheritanceTestSt. For more information, see Creating a Windows Application Project.
Add inherited form
Right click on the InheritanceTest project and then select "Add" and "Add Inherited Form". In the Add New Item dialog box, verify that "Inherited Form" is selected, and then click Open. In the Inherited Selector dialog box, select Form1 from the BaseFormLibrary project as the form you want to inherit from it, and then click OK. This will create a form derived from the form in the BaseformLibrary in the InheritanceTest project. Open the inheritance form in the Windows Form Designer (if it has not been opened). In the Windows Form Designer, the upper corner of the inherited button has a marker symbol (), indicating that they are inherited. Select the SAY Hello button and observe the size adjustment handle. Since this button is protected, the successor can move it, adjust its size, change its title, and other modifications. Choose a private Say Goodbye button, note that it does not have a size adjustment handle. In addition, in the Attributes window, this button's properties will be grayed, indicating that they cannot be modified. Finally, move the mouse pointer to this button, and the tooltips will appear to indicate the inheritance of the control. If you use Visual C #, do the following: Right-click Form1 in the InheritanceTest project, and select the Delete command on the shortcut menu. Add the following code to FORM2 and place it in front of the protected override void dispose (bool disposing) line. The effect of this addition is to make Form2 becomes a startup object. // C #
Static void main ()
{
Application.run (New Form2 ());
} Right-click the InheritanceTest project in Solution Explorer and select "Set to Start Item." Right-click the InheritanceTest project in the Solution Explorer and select Properties. In the InheritanceTest Properties Page dialog box, set the "Start Object" to inherited form (which is likely to be Form2). Press the F5 key to run the application to observe the behavior of the inheritance form.