C#. Windows Forms. Example of developing an inherited form

Windows Forms. Example of developing an inherited form

In this topic, you can explore the process of inheriting a form from a base form. This topic is based on the material of the previous topic:


Contents


Search other resources:

Introduction

This task performs the following basic operations:

  • an inherited (derived) form is created from the base form;
  • the inherited form is configured for a specific job;
  • the inherited form is set as the start form when the application starts.

The inherited form receives the capabilities of the base form and extends (supplements) them with its own features (methods, properties, etc.).

 

Instructions

1. Loading and opening the previous form

At the beginning of work, you need to create a pentagonal form or some other form if you want to inherit this particular form. An example of designing a pentagonal form in Microsoft Visual Studio 2019 is shown here.

In our case, after completing the project of the previous task, the base will be a pentagonal form.

 

2. Customizing the button1 control

Customize button1 (CloseForm) as follows:

  • property Modifiers = protected. This means that button1 is visible in the inherited code (inherited form). The compiler automatically makes changes in the “Form1.Designer.cs” file.

 

3. Adding a new inherited form

To add a new form, follow these steps:

  • in the Project menu, select the “Add Form… (Windows Forms)” command (Figure 1). As a result, the “Add New Item” window will open (Figure 2);
  • in the “Add New Item” window, in the list of template categories, set “Windows Forms”;
  • set “Inherited Form (Windows Forms)” in the list of templates;
  • in the Name field, specify the file name for the new form newForm.cs (Figure 2);
  • select the Add button to go to the next window “Instance Picker” (Figure 3);
  • in the next Instance Picker window, you need to set the base form from which our form will be inherited. This window displays all forms of the current project. In our project, there is only one form Form1, so the choice is small. The base form is Form1 (Figure 3);
  • select the OK button to finish adding the new inherited form.

 

C#. Windows Forms. Microsoft Visual Studio 2019. Command to add a new form

Figure 1. Command to add a new form

 

C#. Windows Forms. Microsoft Visual Studio 2019. Window "Add New Item...". Adding an inherited form

Figure 2. Window “Add New Item…”. Adding an inherited form

 

C#. Windows Forms. Microsoft Visual Studio 2019. Setting up the basic form

Figure 3. Setting up the basic form

 

4. Program code of the inherited form newForm

After completing the previous step, Visual Studio will generate the program code for the new form. The new form has the name (Name) newForm and is described in the newForm.cs and newFormDesigner.cs files.

The form code in the newForm.cs file inherits the base form code Form1:

namespace WindowsFormsApp4
{
  public partial class newForm : WindowsFormsApp4.Form1
  {
    public newForm()
    {
      InitializeComponent();
    }
  }
}

After switching to the new form design mode, you will notice that it has a pentagonal form, like the previous one.

 

5. Setting properties in newForm

In the new form, you need to set the following properties:

  • for the button button1 property Text=”Inherited”;
  • for the button button1 property BackColor = Red;
  • for the form property BackColor = Gold.

 

6. Set derived form as start form

If you now run the project for execution, you will notice that the program is launched from the form Form1 – the base form. To set the newForm as the start form, the base form start line is required

Application.Run(new Form1());

replace with the following line

Application.Run(new newForm());

in the Main() function (file Program.cs).

After the project is launched, the inherited form newForm will be launched.

 


Related topics