[ Home | Syllabus | Course Notes | Assignments | Search]
Two critical questions any software designer should ask are:
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 |
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?
F1: Let's learn something about enum - this is a new type which can have one of three values. Later on we see how to set and use these values
F2: underneath an enumerated type is just a subset of the integers
F3: The type "Units" is a defined in "MyForm"
F4: "Text" is the property of a "TextBox" that stores the value typed in
F5: GroupControls contain collection of other controls - in this case a set of mutually exclusive radiobuttons
F6: Uses value sent in to select the radiobutton to check - consider the value an index in an array of radioButtons
F7: don't want child dialogs to appear in the taskbar (try taking it out)
F8: predefined type for OK/Cancel
F9: that will close dialog with "OK" return./ cancel return
F10: makes three radio buttons mutually exclusive
What might be interesting changes to this program to learn more about dialog controls
Add the ability to change the starting position of the ellipse (maybe even with slider bars?)
Use the color dialog to select the color
Change the shape to a rectangle (checkbox)
Check the border to a dashed line
Since I am curious about slider bars - let's see how to implement that
Find out what a slider bar is - go to the FCL API site
searched for "slider bar .net" choose "trackbar class"
Need to add data members to main form and the dialog form to record starting position (should be of type "Position" or location or point)
Need to add trackbar control to dialog and set it's value
Need to get final position of track bar
Need to set initial value based on current starting point.
Did an incremental build a little/ test a little approach
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);