Dialog Boxes In C#

What is a dialog box?

  • A dialog box 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

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 

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



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 the 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 DialogResul.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.

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

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.

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

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. 

  1. //Font Dialog  
  2. FontDialog fdlg = new FontDialog();  
  3. fdlg.ShowDialog();  
  4. txtEx.Font = fdlg.Font;  
  5. //Color Dialog  
  6. ColorDialog cdlg = new ColorDialog();  
  7. cdlg.ShowDialog();  
  8. txtEx.ForeColor = cdlg.color;   

  • Font Dialog is given below.



  • Color Dialog is given below.

  •  

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.

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all