Dialog Boxes In C#

What is a C# dialog box?

A dialog box in C# is a type of window, which is used to enable common communication or dialog between a computer and its user. A dialog box is most often used to provide the user with the means for specifying how to implement a command or to respond to a question. Windows.Form is a base class for a dialog box.

Sometimes, in a graphical user interface, a window is used to communicate with the user or establish a dialog between the user and the application. This additional window is called a dialog box. It may communicate information to the user; prompt the user for a response or both.

The simplest type of dialog box is the warning which displays a message and may require the user to acknowledge that the message has been read, usually by clicking “OK” or a decision as to whether or not an action should continue by clicking “OK” or “Cancel”.

Some dialog boxes are standard like the warning message or error message. Save the file and enter the password. These are called standard dialog boxes.

A dialog box can also be customized. Such a dialog box is called a custom dialog box.

Dialog boxes are special forms that are non-resizable. They are also used to display the messages to the user. The messages can be error messages, confirmation of the password, confirmation for the deletion of a particular record, Find-Replace utility of the word etc. There are standard dialog boxes to open and save a file, select a folder, print the documents, set the font or color for the text, etc.

MessageBox class is used to display messages to the user. The show() method is used to display a message box with the specified text, caption, buttons, and icon. There are other overloads also available.

For example

DialogResult res = MessageBox.Show("Are you sure you want to Delete", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);  
if (res == DialogResult.OK) {  
    MessageBox.Show("You have clicked Ok Button");  
    //Some task…  
}  
if (res == DialogResult.Cancel) {  
    MessageBox.Show("You have clicked Cancel Button");  
    //Some task…  
}

DialogResult in C#

Type of buttons and icons can be changed by using the appropriate enumerated values.

Dialog boxes are of two types, which are given below.

  1. Modal dialog box
  2. Modeless dialog box

A dialog box that temporarily halts the application and the user cannot continue until the dialog has been closed is called modal dialog box. The application may require some additional information before it can continue or may simply wish to confirm that the user wants to proceed with a potentially dangerous course of action. The application continues to execute only after the dialog box is closed; until then the application halts. For example, when saving a file, the user gives a name of an existing file; a warning is shown that a file with the same name exists, whether it should be overwritten or be saved with different name. The file will not be saved unless the user selects “OK” or “Cancel”.

Another type of dialog box, which is used is a modeless dialog box. It is used when the requested information is not essential to continue, so the Window can be left open, while work continues somewhere else. For example, when working in a text editor, the user wants to find and replace a particular word. This can be done, using a dialog box, which asks for the word to be found and replaced. The user can continue to work, even if this box is open.

Dialog box can be configured to constant by setting FormBorderStyle property to FormBorderStyle.FixedDialog, setting MinimizeBox and MaximizeBox property to false. Framework Class Library(FCL) does not provide class as Dialog. The developer creates custom dialog classes by deriving type from System.Windows.Form base class.

Model dialog is displayed, using ShowDialog() method. Modeless dialog boxes are displayed, using Show() method.

Common Dialog Box

The dialog boxes that are used, which are common to all Windows Application. It performs common tasks like saving a file, choosing a font etc. This provides a standard way to the Application interface.

The examples are given below.

  • FontDialog
  • ColorDialog
  • OpenDialog
  • SaveDialog

These dialog boxes are implemented by an operating system, so they can be shared across all the Application that runs on that operating system (Windows).

Steps to use common dialog box

  • Instantiate the required common dialog box.
  • Set the properties of common dialog box, if required.
  • Call its ShowDialog() method to invoke the dialog box.

ShowDialog() returns an enumerated type called DialogResult. It defines the identifiers, which indicates which button was clicked. For example, DialogResult.OK and DialogResult.Cancel are some values that indicates OK or Cancel button were clicked respectively.

Open File Dialog Box

The OpenFileDialog allows you to choose a file to be opened in an Application.

For example, the code is given below.

OpenFileDialog ofd = new OpenFileDialog();  
ofd.Title = "Open a Text File";  
ofd.Filter = "Text Files (*.txt) | *.txt | All Files(*.*) | *.*"; //Here you can filter which all files you wanted allow to open  
DialogResult dr = ofd.ShowDialog();  
if (dr == DialogResult.OK) {  
    StreamReader sr = new StreamReader(ofd.FileName);  
    txtEx.Text = sr.ReadToEnd();  
    sr.Close();  
}

Open File Dialogbox

Save File Dialog Box

The SaveFileDialog box is used to allow the user to select the destination and name of the file to be saved.

For example, the code is given below.

SaveFileDialog sfdlg = new SaveFileDialog();  
sfdlg.Filter = "Text Files (*.txt) | *.txt"; //Here you can filter which all files you wanted allow to open  
if (sfdlg.ShowDialog() == DialogResult.OK) {  
    // Code to write the stream goes here.  
} 

Save Dialogbox

The content can be saved to the file, using appropriate class like StreamWriter class in the case of Text Editor Application.

Font and Color Dialog Boxes

FontDialogBox is used to allow the user to select font settings. The ColorDialogBox is used to allow the user to select a color.

For example, the code is given below.

//Font Dialog  
FontDialog fdlg = new FontDialog();  
fdlg.ShowDialog();  
txtEx.Font = fdlg.Font;  
//Color Dialog  
ColorDialog cdlg = new ColorDialog();  
cdlg.ShowDialog();  
txtEx.ForeColor = cdlg.color;   

Font Dialogbox

Output of Color Dialog is given below.

Color Dialogbox

In either case, the properties and methods are available. They can be used to get the selected values or set the values for the dialog box.

Custom Dialog Box

Even though common dialog boxes are useful, they do not support the requirements of domain-specific dialog boxes. Developer need to create their own dialog boxes.

Following steps represent the process of creating Custom Dialog Box

  1. Add a form to your project by right clicking the project in Solution Explorer, point to Add and then click Windows Form.
  2. In the properties Window, change the FormBorderStyle property to FixedDialog.
  3. Customize the appearance of the form, as required.
  4. Add controls into this form.

Note

Use ShowDialog() method to open the form as modal dialog box. This method can be used for the custom dialog boxes too. Show() method is used to open the model's dialog box.


Similar Articles