Dialogs

In this part of the Mono Winforms tutorial, we will talk about dialogs.

Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.

There are essentially two types of dialogs. Predefined dialogs and custom dialogs.

FolderBrowserDialog

This dialog prompts the user to select a folder.

folderbrowserdialog.cs
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private StatusBar statusbar;

    public MForm() {
        Text = "FolderBrowserDialog";
 
        toolbar = new ToolBar();
        open = new ToolBarButton();

        statusbar = new StatusBar();
        statusbar.Parent = this;

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        Controls.Add(toolbar);

        CenterToScreen();
    }


    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       FolderBrowserDialog dialog = new FolderBrowserDialog();

       if (dialog.ShowDialog(this) == DialogResult.OK) {
           statusbar.Text = dialog.SelectedPath;
       }
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

We have a toolbar and one toolbar button. By clicking on the button, the FolderBrowserDialog appears on the screen. The name of the selected folder is shown in the statusbar.

FolderBrowserDialog dialog = new FolderBrowserDialog();

The FolderBrowserDialog is created.

if (dialog.ShowDialog(this) == DialogResult.OK) {
    statusbar.Text = dialog.SelectedPath;
}       

The ShowDialog() method shows the dialog on the screen. If we click on the OK button of the dialog, the selected directory path is shown on the statusbar.

FolderBrowserDialog
Figure: FolderBrowserDialog

ColorDialog

This dialog displays available colors along with controls that enable the user to define custom colors.

colordialog.cs
 using System;
 using System.Drawing;
 using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private Color color;

    private int rectWidth = 100;
    private int rectHeight = 100;
    private Rectangle r;

    public MForm() {
        Text = "ColorDialog";

        color = Color.Blue;
 
        toolbar = new ToolBar();
        open = new ToolBarButton();

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        LocateRect();

        SetStyle (ControlStyles.ResizeRedraw, true);
        Controls.Add(toolbar);
        Paint += new PaintEventHandler(OnPaint);

        CenterToScreen();
    }


    void OnPaint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      LocateRect();

      SolidBrush brush = new SolidBrush(color);

      g.FillRectangle(brush, r);
    }


    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       ColorDialog dialog = new ColorDialog();
       if (dialog.ShowDialog(this) == DialogResult.OK) {
          color = dialog.Color;
          Invalidate();
       }
    }

    void LocateRect() {
        int x = (ClientSize.Width - rectWidth) / 2;
        int y = (ClientSize.Height - rectHeight) / 2;
        r = new Rectangle(x, y, rectWidth, rectHeight);
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

In this code example, we use the ColorDialog to choose a color for a rectangle that is located in the middle of the form control.

color = Color.Blue;

At the beginning, the color of the rectangle is blue. We use the color variable to determine the color of the rectangle.

ColorDialog dialog = new ColorDialog();

The ColorDialog is created.

if (dialog.ShowDialog(this) == DialogResult.OK) {
   color = dialog.Color;
   Invalidate();
}

The code shows the color dialog. If we click on the OK button, we get the selected color and call the Invalidate() method. The method invalidates the entire surface of the control and causes the control to be redrawn. The result is that the rectangle is drawn with a new color value.

ColorDialog
Figure: ColorDialog

FontDialog

The FontDialog is used to select fonts.

fontdialog.cs
 using System;
 using System.Drawing;
 using System.Windows.Forms;

class MForm : Form {

    private Label text;
    private ToolBar toolbar;
    private ToolBarButton open;

    public MForm() {
        Text = "FontDialog";

        text = new Label();
        text.Parent = this;
        text.Text = "Winforms tutorial";

        LocateText();
 
        toolbar = new ToolBar();
        toolbar.Parent = this;
        open = new ToolBarButton();

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        text.AutoSize = true;
        Resize += new EventHandler(OnResize);

        CenterToScreen();
    }

    void OnResize(object sender, EventArgs e){
       LocateText();
    }

    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       FontDialog dialog = new FontDialog();
       if (dialog.ShowDialog(this) == DialogResult.OK) {
          text.Font = dialog.Font;
          LocateText();
       }
    }

    void LocateText() {
        text.Top = (this.ClientSize.Height - text.Height) / 2;
        text.Left = (this.ClientSize.Width - text.Width) / 2;
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

We draw some text in the middle of the form control. We use the font dialog to change font for this text.

FontDialog dialog = new FontDialog();

A FontDialog is created.

if (dialog.ShowDialog(this) == DialogResult.OK) {
   text.Font = dialog.Font;
   LocateText();
}

When we click on the OK button, we set a newly selected font for the Label control. Because the size of the text changes with different fonts, we must call the LocateText() method, which locates the text in the middle of the form control.

FontDialog
Figure: FontDialog

OpenDialog

This dialog is used to open files.

opendialog.cs
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private TextBox textbox;

    public MForm() {
        Text = "OpenFileDialog";

        toolbar = new ToolBar();
        open = new ToolBarButton();

        textbox = new TextBox();
        textbox.Multiline = true;
        textbox.ScrollBars = ScrollBars.Both;
        textbox.WordWrap = false;
        textbox.Parent = this;
        textbox.Dock = DockStyle.Fill;
       

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        Controls.Add(toolbar);
        Controls.Add(textbox);

 	CenterToScreen();
    }

    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       OpenFileDialog dialog = new OpenFileDialog();
       dialog.Filter = "C# files (*.cs)|*.cs";

       if (dialog.ShowDialog(this) == DialogResult.OK) {
           StreamReader reader = new StreamReader(dialog.FileName);
           string data = reader.ReadToEnd();
           reader.Close();
           textbox.Text = data;
       }
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

We use the OpenDialog control to open C# source files. We have a TextBox control, where we display the file.

OpenFileDialog dialog = new OpenFileDialog();

The OpenDialog is created.

dialog.Filter = "C# files (*.cs)|*.cs";

We set the Filter property to C# source files. Only C# files can be chosen with this dialog instance.

if (dialog.ShowDialog(this) == DialogResult.OK) {
    StreamReader reader = new StreamReader(dialog.FileName);
    string data = reader.ReadToEnd();
    reader.Close();
    textbox.Text = data;
}

After clicking OK, we read the contents of the chosen file and put it into the TextBox control.

OpenDialog
Figure: OpenDialog

In this part of the Mono Winforms tutorial, we showed various dialogs.