C#. Windows Forms. An example of creating a modeless form in a Windows Forms application. Modal and modeless windows

An example of creating a modeless form in a Windows Forms application. Modal and modeless windows

1. General information about modal and non-modal forms

Any more or less serious program uses dialog boxes to organize the user interface. In the graphical interface of the Windows operating system, there are two types of dialog windows (dialog boxes):

  • modal windows;
  • modeless windows.

Modal windows are the most common type of window. If a modal window is opened, then to get to the main program, it definitely needs to close. Without closing the modal window, it is impossible to switch to the program that called it. In Windows Forms programs, it is possible that one modal window opens another (next) modal window. Thus, a chain of calls is obtained.

As a rule, a modal window contains buttons of the type OK, Cancel (or others), when clicked, this window closes.

An example of a modal window is the Open… command of the Microsoft Word editor, which opens a file selection window. Until a file is selected or one of the cancel buttons is pressed, it will not be possible to go to the editor.

When you open a modeless window, it is possible to switch from a window to the main program without closing this window, and, conversely, from the main program you can return to the modeless window again. But at the same time, a visually modeless window is always placed above the main program. As a rule, a modeless window contains one Close button that closes it.

An example of a modeless window is the “Insert Symbol” command of the Microsoft Word editor, which allows you to insert a symbol into a document. From the symbol insertion window, you can switch to the main document and vice versa, from the document you can switch to a modeless window.

 

2. Features of working with modeless windows in Windows Forms

In Windows Forms technology, the main program corresponds to a form – an instance of a class inherited from the Form class. Also, some form corresponds to a modal or non-modal window.

When working with modeless windows, the following features were noticed:

  • in a modeless window there is one Close button that closes it;
  • the modeless window is called from the main program using the Show() method of the Form class (to call the modal window you need to use the ShowDialog() method).

The following code gives examples of calling a modal and non-modal window

// 1. Call a modal window
dlgResult = modalForm.ShowDialog();

// Processing the return result, which is placed in dlgResult
// ...

// 2. Call a modeless window
nonModalForm.Show(); // nothing needs to be processed here

As you can see from the above code, the problem is a way of exchanging information between the main program and the modeless window. The main program corresponds to the main form, the modeless window corresponds to the modeless form.

 

3. Information exchange methods between the main form and the modeless form

When using modeless forms, it is important to exchange information between the main program (main form) and the modeless form.

Two situations are possible here:

  • transfer of information from the main form to modeless form. This situation is resolved without problems, since an instance of a modeless form is created in the main form. Through this instance, components of a modeless form are accessed in a standard way;
  • passing information from a modeless form to the main form. Here the problem arises: how to access an instance of the main form from a modeless form.

To pass data (information) from a modeless form to the main form, you can use one of the following methods:

  1. With the help of a static reference to the main form. As you know, static variables are global, so they can conveniently be used to exchange information between different shared instances or processes. Having a reference to the main form, you can easily access the components of this form. This method is the easiest to use.
  2. Using the event engine. With this method, an event is declared in a modeless form that occurs every time the information needs to be passed to the main form. Nevertheless, the handler of this event is placed in the main form. If it is necessary to pass information from the modeless form to the main form, then an event occurs. As a result, the handler of this event is called in the main form. This handler receives an object of type “modeless dialog box” that generated the event. Having access to the modified object of a modeless window, you can update the information in the main form.
  3. By writing information to a temporary file. As you know, files are like static variables. Therefore, writing a portion of information to a file in a non-modal form, you can read it in the main form. This method is not effective, because it works more slowly (reading from storage media takes longer than from RAM).

 

4. An example of developing a demo program using a modeless form

This topic describes an example of creating and using a modeless form. To transfer information from a modeless form to the main form, the method of using a static reference to the main form is used. With this method, before invoking a modeless form, the value of the instance of the main form is stored in a static variable. If it is necessary to transfer information from a modeless form to the main form, then a static variable is accessed.

The demonstration program consists of two forms: the main and modeless. A modeless form is called from the main form. From a non-modal form, information is copied from the input field to the corresponding input field of the main form.

4.1. Create a project of Windows Forms type

After creating a project using the Windows Forms template, the main form is automatically created, an instance of which is called Form1.

 

4.2. Development of the main form

Create a main form and place the following controls on it:

  • the control of Button type. A button instance is created with the name button1;
  • the control of Label type. An instance with the name label1 is created;
  • the control of TextBox type. An instance with the name textBox1 is created.

Set the properties of the form and controls as shown in Figure 1:

  • in the Form1 control, property Text = “Main Form” (hereinafter Form1.Text);
  • button1.Text = “Show Modeless Form”;
  • label1.Text = “Text”;
  • textBox1.Modifiers = Public. The value of Public is necessary for this control to be visible from a modeless form.

You also need to correct the dimensions of the main form.

After setting, the main form will look like as shown in Figure 1.

C#. Windows Forms. Creating a modeless form. The main form

Figure 1. The main form

 

4.3. Create a new form Form2

Creating a new form is carried out in a standard way using commands

Project -> Add Form (Windows Forms...)

In the window that opens, you need to specify the template “Form (Windows Forms)”. The default file name is Form2.cs. As a result, an instance of the form named Form2 will be created.

 

4.4. Designing the Form2

Place the following controls on Form2:

  • one control of Label type. An object named label1 will be created;
  • two controls of Button type. Two objects with the names button1, button2 will be created;
  • one control of TextBox type. An instance with the name textBox1 will be created.

Configure the following properties of Form2 control:

  • Form2.Text = “Modeless Form”;
  • button1.Text = “Copy to Form1”;
  • button2.Text = “Close”;
  • label1.Text = “Text”;
  • textBox1.Modifiers = Public.

After setting up, the modeless form will look as shown in Figure 2.

C#. Windows Forms. Modeless form after customizing controls

Figure 2. Modeless Form2 form after customizing controls

 

4.5. Entering a static reference to Form1

In the text of the class Form1, you need to enter a static variable named form1:

...

public partial class Form1 : Form
{
  // Reference to the current instance of the main form - save in
  // a static variable that is accessible from any part of the program
  public static Control form1;

  public Form1()
  {
    InitializeComponent();
  }
}

...

A static variable is of type Control. All controls are inherited from the Control class. The same applies to the form.

 

4.6. Programming the Click event handler of the button1 control of form Form1

When you click button1 (Show Modeless Form), a window of a non-modal form opens and the text from the input field of the main form (Form1.textBox1.Text) is entered in the input field of a non-modal form (Form2.textBox1.Text).

To complete this step, you first need to activate Form1. Then you need to program the Click event of the button1 control placed on Form1.

The text of the event handler is as follows:

// The Click event handler for the button1 control.
// Performs following actions:
// - invoke the modeless form;
// - in the textBox1 a text from the Form1 is written.
private void button1_Click(object sender, EventArgs e)
{
  // 1. Declare an instance of modeless form
  Form2 f2 = new Form2();

  // 2. Write current instance to form1 static variable
  form1 = this;

  // 3. Display the modeless form
  f2.Show();

  // 4. Write from Form2.TextBox1 a text from the main form
  f2.textBox1.Text = this.textBox1.Text;
}

 

4.7. Programming the event handler of the Click button button1 of the form Form2

When you click button1 (Copy to Form1), the text from the input field of a modeless form (Form2.textBox1.Text) should be copied to the corresponding input field of the main form (Form1.textBox1.Text).

First you need to activate the form Form2 and create a handler for the Click event of the button1 control.

The text of the event handler is as follows:

...

// The following operations are performed in this handler:
// - the content of Form2.textBox1 is copied to Form1.textBox1
private void button1_Click(object sender, EventArgs e)
{
  // Get the textBox1 control of the main form.
  // To implement this, all the controls of the main form are bypassed.
  for (int i = 0; i < Form1.form1.Controls.Count; i++)
  {
    // Search for the textBox1 control
    if ((Form1.form1.Controls[i] is TextBox)&&
        (Form1.form1.Controls[i].Name == "textBox1"))
    {
      // if textBox1 is found, then write the text from Form2 to it.
      Form1.form1.Controls[i].Text = this.textBox1.Text;
      break;
    }
  }
}

...

In the event handler based on the static reference form1, a control element of the TextBox type with the name textBox1 is called cyclically (just in case there are several textBox-es in the main form).

 

4.8. Programming the Click event handler for the button2 control

When you click button2, you need to close the modeless form. The text of the corresponding handler is as follows:

...

// Close the modeless form
private void button2_Click(object sender, EventArgs e)
{
  this.Close();
}

...

As you can see from the above code, a modeless form closes just like a modal one.

 

4.9. Run the program

After completing the instructions, you can run the program. During program execution, using the “Show Modeless Form” button, you can invoke several modeless forms from the main form (Figure 3). In this case, from secondary modeless forms, information can be transferred to the main program one by one.

C#. Windows Forms. The case when several modeless forms are called from the main form

Figure 3. The case when 4 modeless forms are called from the main form


 

0
fb-share-icon20