C#. Windows Forms. Example of creating a new form and calling the application




Example of creating and calling a new form in C# application


Contents


Search other websites:

Task

Develop a demo application, which calls secondary form from main form based on scheme showed on Figure 1. Application realizes the interconnection between different forms, which can be the dialog windows.

In the main form Form1 need to place:

  • the control of Label type for showing of returning result from the secondary form;
  • the control of Button type for calling of the secondary form.
  • In the secondary form Form2 need to place:
  • the control of Label type for showing of form’s title.
  • two controls Button type for confirmation or not confirmation selection (action) in a secondary form.

MS Visual Studio interconnection scheme forms

Fig. 1. The interconnection scheme between forms

 


Instructions

1. Run MS Visual Studio. Create the project

A detailed example of Windows Forms Application developing is described  here. Save the project in any folder.

After creating of application we have one form. The program code of form is placed to “Form1.cs” file (Fig. 2).

Visual Studio C# main form application

Fig. 2. The main form of application

 

2. Developing the main form

From the controls palette “Toolbox” we place on the form:

  • the control of Button type;
  • the control of Label type.

Automatically two objects with the names button1 and label1 are created.

In the control of type “Button“, set the “Text” property to value “Show Form 2“.

Property Text of Label type control set to value “Result = “.

After changes, the main form Form1 of application will look as shown in Figure 3.

Visual Studio C# Main form application

Fig. 3. Main form of application after changes

 

3. Developing the secondary form

For developing secondary form in C# we can use the several methods.

Method 1.

To add secondary form to project by this method we need to call the command (Fig. 4)

Project -> Add Windows Form...

C# Windows Forms add new form

Fig. 4. Command “Add Windows Form…” to add a new form

As result, the window “Add New Item – Windows Forms Application1” will be opened. In this window we select the element “Windows Form” (Fig. 5). Leave the form’s name as “Form2.cs“.

Visual Studio window adding new form

Fig. 5. Window of adding a new form to project

After pressing at “Add” button the new form will be added to the project (Fig. 6).

Visual Studio C# form file "Form2.cs"

Fig. 6. The newly-created form Form2 and file “Form2.cs

Method 2.

Also, we can add the new form to project with the help of corresponding command from the context menu (Fig. 7).

The sequence of actions is as follows:

  • in the “Solution Explorer” click of the right mouse button at the title of application “WindowsFormsApplication1“;
  • select submenu Add;
  • in submenu Add select the command “Windows Form…“.

Visual Studio C# add form Solution Explorer

Fig. 7. Adding the new form from Solution Explorer

As a result, the window, which is displayed at figure 5, will be opened.

Using tools “Toolbox“, you need to create secondary form Form2 as is shown in figure 8. This structure of form corresponds to the task. In the same way, we have such controls on the Form2: label1, button1, button2.

Visual Studio C# secondary form Form2

Fig. 8. Secondary form Form2

4. Programming of event handling of click at the buttons “OK” and “Cancel” of form Form2

We program the event of click at button “OK“. In the program code of event handling button1_Click() (button “OK“), type the following string:

this.DialogResult = DialogResult.OK;

this means that the result of the return in Form2 have “OK“.

Likewise in the event handler button2_Click we type:

this.DialogResult = DialogResult.Cancel;

This means the choice of a button “Cancel” (button2).



After changes, listing of program code of file “Form2.cs” will as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
    } 
    private void button1_Click(object sender, EventArgs e)
    {
      this.DialogResult = DialogResult.OK;
    }

    private void button2_Click(object sender, EventArgs e)
    {
      this.DialogResult = DialogResult.Cancel;
    }
  }
}

To go to the program code of form “Form2” (file “Form2.cs“) we need use “Solution Explorer“. For this, in “Solution Explorer“, we need to call the context menu for form “Form2” and select the command “View Code” from this menu (Fig. 9).

02_02_00_006_09_

Fig. 9. Command “View Code” to go to the program code mode

 

5. Calling Form2 from the main form

According to problem condition, to call Form2 from Form1, we need to program the event of clicking at the button “Show Form 2“.

The program code of event handling will be as follows:

...
private void button1_Click(object sender, EventArgs e)
{
  Form2 f = new Form2(); // creating Form2 object
  if (f.ShowDialog() == DialogResult.OK) // call the form Form2
  {
    label1.Text = "Result = OK!";
  }
  else
  {
    label1.Text = "Result = Cancel!";
  }
}
...

In the above code listing, first of all, the class instance of Form2 type will be created. The conditional jump “if” calls the dialog box in Form2 with the help of string

f.ShowDialog()

Function ShowDialog () displays a form’s window and keeps it open as long as the user does not make any selection. After user selection of some command, window closes with the returning code. It checks the return code with known constants of class DialogResult. After checking, the message about selected action by user in Form2, is displayed.

Listing of the all code of Form1 is the next:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Form2 f = new Form2();
      if (f.ShowDialog() == DialogResult.OK)
      {
        label1.Text = "Result = OK!";
      }
      else
      {
        label1.Text = "Result = Cancel!";
      }
    }
  }
}

 

6. Run the application

After performing actions we can run the application and test it.


Related topics