Standard dialogs - Object-oriented programming

Standard dialogs

The FCL library has a set of standard modal forms that allow you to perform standard actions:

• OpenFileDialog - dialog for selecting and opening a file for reading data;

• SaveFileDialog - the dialog for selecting and opening a file for writing data;

• PrintDialog - the print dialog;

• PrintPreviewDialog - print result preview dialog;

• ColorDialog - color selection dialog;

• FontDialog - the font selection dialog;

• FolderBrowserDialog dialog for working with directories.

All these forms are supported by the standard windows of the operating system. The logic of working with these forms is the same and consists in the following:

• Description and creation of the object of the required form class;

• setting properties of this object for its visual representation and logic of operation;

• call the given method of the ShowDialog () method and compare the result with the value of DialogResult.OK;

• If the completion is successful, then the values ​​of the desired dialog properties are stored in application variables.

For example, the OpenFileDialog dialog has the following main properties:

• Title - the title of the dialog form;

• Filter - "Description 1 | filter 1 | description2 | filter2 "; for example: txt files (* .txt) | * .txt | All files (V) | Vd

• Filterlndex - the default filter index;

• InitialDirectory - the initial folder to view;

• FileName - the name of the selected file (selection result).

The following is an example of using the OpenFileDialog dialog to read data from the selected file:

string input;

OpenFileDialog oFileDIg = new OpenFileDialog ();

oFileDIg. InitialDirectory = c: & quot ;;

oFileDIg.Filter = "txt files (* .txt) | * .txt | All files (*. *) | *. * & quot ;;

if (oFileDIg.ShowDialog () == DialogResult.OK) {

// read data from the selected file

StreamReader sr = File.OpenText (oFileDlg.FileName);

input = sr.ReadToEnd ();


The form of the created dialog form is shown in Fig. 8.5.

File Open Dialog

Fig. 8.5. Open file dialog

In addition to the standard dialogs listed above, a simple modal message box is often used, which can be displayed on the screen using the overloaded Show () method (12 method variants) of the static class System.Windows.Forms.MessageBox. Like the usual modal form, this message window returns a result of type DialogResult.

For example, one of the Show methods has the following signature: DialogResult Show (string, string, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton), where:

• The MessageBoxButtons enumeration is a combination of buttons on the form, such as Ok, OkCancel, RetryCancel, Yes, No, YesNoCancel, AbortRetrylgnore;

• The MessageBoxIcon enum is an icon displayed in a form, such as None, Hand, Question (?), Exclamation (!), Asterisk (*), Stop, Error, , Warning (!), Information (i);

• enumeration MessageBoxDefaultButton - the number of the button that will be selected by default; valid values: Button1, Button2, Button3.

Below is an example of calling a message window with this set of parameters:

MessageBox.Show ("Continue the execution of the program?", "Text editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton .Button2);

The result of this method is shown in Fig. 8.6.

Simple message dialog

Fig. 8.6. Simple message dialog

Ошибка в функции вывода объектов.