Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Dialog Boxes

Two critical questions any software designer should ask are:

  1. Which object should remember this information (state)
  2. Which object is responsible for making this happen. (action)

Common Dialog Classes Defined in System.Windows.Forms

 

Class

Dialog Type

ColorDialog

Color dialog boxes for choosing colors

FontDialog

Font dialog boxes for choosing fonts

OpenFileDialog

Open File dialog boxes for choosing files

PageSetupDialog

Page Setup dialog boxes for entering page setup parameters

PrintDialog

Print dialog boxes for entering print parameters

SaveFileDialog

Save File dialog boxes for entering file names


An Example (code here)

using System;
using System.Windows.Forms;
using System.Drawing;

class MyForm : Form
{
    enum Units {
        Inches,
        Centimeters,
        Pixels
    }

    int MyWidth = 400;
    int MyHeight = 200;
    Units MyUnits = Units.Pixels; // enumerated values "static" members

    MyForm ()
    {
        // Set the form's title
        Text = "Dialog Demo";

        // Set the form's size
        ClientSize = new Size (640, 480);

        // Create a menu
        MainMenu menu = new MainMenu ();
        MenuItem item = menu.MenuItems.Add ("&Options");
        item.MenuItems.Add ("Edit Ellipse...",
            new EventHandler (OnEditEllipse)); // will create dialog
        item.MenuItems.Add ("-");
        item.MenuItems.Add ("E&xit", new EventHandler (OnExit));

        // Attach the menu to the form
        Menu = menu; // don't forget to attach to this window
    }

    // Handler for the Edit Ellipse command
    void OnEditEllipse (object sender, EventArgs e)
    {
        // Create the dialog
        MyDialog dlg = new MyDialog (); // defined later on

        // Initialize the dialog
        dlg.UserWidth = MyWidth; // pass in current values
        dlg.UserHeight = MyHeight;
        dlg.UserUnits = (int) MyUnits;

        // Display the dialog
        if (dlg.ShowDialog (this) == DialogResult.OK) {
            // If the dialog was dismissed with the OK button,
            // extract the user input and repaint the form
            MyWidth = dlg.UserWidth;// get back any changes
            MyHeight = dlg.UserHeight;
            MyUnits = (MyForm.Units) dlg.UserUnits;
            Invalidate (); // what would happen if removed?
        }

        // Dispose of the dialog
        dlg.Dispose ();// dialog uses graphics objects
    }

    // Handler for the Exit command
    void OnExit (object sender, EventArgs e)
    {
        Close ();
    }

    // OnPaint handler
    protected override void OnPaint (PaintEventArgs e)
    {
        int multiplier = 1;

        switch (MyUnits) {

        case Units.Inches:
            e.Graphics.PageUnit = GraphicsUnit.Inch;
            break;

        case Units.Centimeters:
            e.Graphics.PageUnit = GraphicsUnit.Millimeter;
            multiplier = 10;
            break;

        case Units.Pixels:
            e.Graphics.PageUnit = GraphicsUnit.Pixel;
            break;
        }

        SolidBrush brush = new SolidBrush (Color.Magenta);
        e.Graphics.FillEllipse (brush, 0, 0, MyWidth * multiplier,
            MyHeight * multiplier);
        brush.Dispose ();
    }

    static void Main () 
    {
        Application.Run (new MyForm ());
    }
}

class MyDialog : Form
{
    Label WidthLabel; // "controls" for this form
    Label HeightLabel;
    TextBox WidthBox;
    TextBox HeightBox;
    GroupBox UnitsGroup; // Groups the following three radio buttons
    RadioButton InchesButton; // see how later on
    RadioButton CentimetersButton;
    RadioButton PixelsButton;
    Button OKButton;
    Button NotOKButton;

    public int UserWidth // define the properties
    {
        get { return Convert.ToInt32 (WidthBox.Text); }
        set { WidthBox.Text = value.ToString (); }
    }

    public int UserHeight
    {
        get { return Convert.ToInt32 (HeightBox.Text); }
        set { HeightBox.Text = value.ToString (); }
    }

    public int UserUnits
    {
        get
        {
            for (int i=0; i<UnitsGroup.Controls.Count; i++) {
                RadioButton button =
                    (RadioButton) UnitsGroup.Controls[i];
                if (button.Checked)
                    return i;                
            }
            return -1;
        }
        set
        {
            RadioButton button =
                (RadioButton) UnitsGroup.Controls[value]; 
            button.Checked = true; // what if false?
        }
    }

    public MyDialog () // construct the object' appearance
    {
        // Initialize the dialog's visual properties
        ClientSize = new Size (296, 196);
        StartPosition = FormStartPosition.CenterParent;
        FormBorderStyle = FormBorderStyle.FixedDialog;
        MaximizeBox = false;
        MinimizeBox = false;
        Text = "Edit Ellipse";
        ShowInTaskbar = false; 

        // Create the dialog's controls
        WidthLabel = new Label ();
        WidthLabel.Location = new Point (16, 16);
        WidthLabel.Size = new Size (48, 24);
        WidthLabel.Text = "Width";

        HeightLabel = new Label ();
        HeightLabel.Location = new Point (16, 48);
        HeightLabel.Size = new Size (48, 24);
        HeightLabel.Text = "Height";

        WidthBox = new TextBox ();
        WidthBox.Location = new Point (64, 12);
        WidthBox.Size = new Size (96, 24);
        WidthBox.TabIndex = 1;

        HeightBox = new TextBox ();
        HeightBox.Location = new Point (64, 44);
        HeightBox.Size = new Size (96, 24);
        HeightBox.TabIndex = 2;

        UnitsGroup = new GroupBox (); // has visible presence
        UnitsGroup.Location = new Point (16, 76);
        UnitsGroup.Size = new Size (144, 100);
        UnitsGroup.Text = "Units";

        InchesButton = new RadioButton ();
        InchesButton.Location = new Point (16, 24);
        InchesButton.Size = new Size (112, 16);
        InchesButton.Text = "Inches";

        CentimetersButton = new RadioButton ();
        CentimetersButton.Location = new Point (16, 48);
        CentimetersButton.Size = new Size (112, 16);
        CentimetersButton.Text = "Centimeters";

        PixelsButton = new RadioButton ();
        PixelsButton.Location = new Point (16, 72);
        PixelsButton.Size = new Size (112, 16);
        PixelsButton.Text = "Pixels";

        OKButton = new Button ();
        OKButton.Location = new Point (184, 12);
        OKButton.Size = new Size (96, 24);
        OKButton.TabIndex = 3;
        OKButton.Text = "OK";
        OKButton.DialogResult = DialogResult.OK;

        NotOKButton = new Button ();
        NotOKButton.Location = new Point (184, 44);
        NotOKButton.Size = new Size (96, 24);
        NotOKButton.TabIndex = 4;
        NotOKButton.Text = "Cancel";
        NotOKButton.DialogResult = DialogResult.Cancel;

        AcceptButton = OKButton;
        CancelButton = NotOKButton;

        // Add the controls to the dialog
        Controls.Add (WidthLabel);
        Controls.Add (HeightLabel);
        Controls.Add (WidthBox);
        Controls.Add (HeightBox);
        Controls.Add (UnitsGroup);
        UnitsGroup.Controls.Add (InchesButton);
        UnitsGroup.Controls.Add (CentimetersButton);
        UnitsGroup.Controls.Add (PixelsButton);
        Controls.Add (OKButton);
        Controls.Add (NotOKButton);
    }
}

 

//// where are all the click handlers for the controls?



What might be interesting changes to this program to learn more about dialog controls


Since I am curious about slider bars - let's see how to implement that

Here are the changes (in red with a line on either side for context)
Full source code is here. Improved version here (does not hard code in size of parent)

//// around line 15
    Units MyUnits = Units.Pixels;
    Point MyStart = new Point(0,0); // added

    MyForm ()
//// Around line 46
	dlg.UserUnits = (int) MyUnits;
	MessageBox.Show("Setting X " + MyStart.X.ToString() + " and Y " + MyStart.Y.ToString());
	dlg.Start = MyStart;

        // Display the dialog
//// Around line 54
	MyUnits = (MyForm.Units) dlg.UserUnits;
	MyStart = dlg.Start;
      Invalidate ();
//// Around line 90
	SolidBrush brush = new SolidBrush (Color.Magenta);
      e.Graphics.FillEllipse (brush, MyStart.X, MyStart.Y, MyWidth * multiplier,
            MyHeight * multiplier);
      brush.Dispose ();
//// Around line 112
    Button NotOKButton;
    TrackBar xStart; // added
    TrackBar yStart; // added


    public Point Start
    {
       get {
		Point start = new Point();
		start.X = xStart.Value;
		start.Y = yStart.Value;
		return start;
       }
       set {
		xStart.Value = value.X;
		yStart.Value = value.Y;
	}
    }

    public int UserWidth
//// Around line 149
     ClientSize = new Size (296, 296); // need more room
//// Around line 213
      NotOKButton.DialogResult = DialogResult.Cancel;

	xStart = new TrackBar();
	xStart.Location = new Point(10, 220);
	xStart.Size = new Size(200,5);
	xStart.Minimum = 0;
	xStart.Maximum = 620;

	yStart = new TrackBar();
	yStart.Location = new Point(10, 260);
	yStart.Size = new Size(200,5);
	yStart.Minimum = 0;
	yStart.Maximum = 480;

      AcceptButton = OKButton;
//// around line 223
      UnitsGroup.Controls.Add (PixelsButton);
	Controls.Add (xStart);
	Controls.Add (yStart);
      Controls.Add (OKButton);






 

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.