Creating Dialogs

I l @ ve RuBoard

Under .NET, the interpretation of what is and is not a dialog is largely dependant on the user interface you choose to use on it. As was mentioned before, a modal dialog is something that requires the user to finish his or her interaction before continuing with the rest of the application. Therefore, dialogs become less and less useful as they get more complex. You must make very careful decisions about what functionality goes into the application's forms and what goes in its dialogs.

Modal and Modeless Dialogs

A dialog can be created in the same way as a form, by dragging tools from a tool palette or by laying out components by hand. The Form class has a property called Modal that will cause the form to be displayed modally when set to true . The Form method ShowDialog sets this property for you and allows you to display forms in a modal fashion. Listing 3.2.6 shows the same dialog form employed in these two different modes.

Listing 3.2.6 Modes.cs : Creating Modal and Modeless Dialogs
 1: using System;  2: using System.ComponentModel;  3: using System.Drawing;  4: using System.Windows.Forms;  5: using System.Collections;  6:  7: class ADialog : Form  8: {  9: 10:     private Button ok,can; 11: 12:     public bool Modeless 13:     { 14:         set 15:         { 16:             if(value) 17:             { 18:                 ok.Click += new EventHandler(OnCloseModeless); 19:                 can.Click += new EventHandler(OnCloseModeless); 20:             } 21:         } 22:     } 23: 24:     void OnCloseModeless(Object sender, EventArgs e) 25:     { 26:         this.Close(); 27:     } 28: 29:     public ADialog() 30:     { 31:         this.Location = new Point(100,100); 32:         this.Size=new Size(200,100); 33:         this.BorderStyle = FormBorderStyle.FixedDialog; 34: 35:         ok = new Button(); 36:         ok.Text = "OK"; 37:         ok.DialogResult = DialogResult.OK; 38:         ok.Location = new Point(20,10); 39:         ok.Size = new Size(80,25); 40:         this.Controls.Add(ok); 41: 42:         can = new Button(); 43:         can.Text = "Cancel"; 44:         can.DialogResult = DialogResult.OK; 45:         can.Location = new Point(110,10); 46:         can.Size = new Size(80,25); 47:         this.Controls.Add(can); 48:     } 49: } 50: 51: 52: class AnApp : Form 53: { 54:     void OnModeless(Object sender, EventArgs e) 55:     { 56:         ADialog dlg = new ADialog(); 57:         dlg.Text = "Modeless"; 58:         dlg.Modeless = true; 59:         dlg.Show(); 60:     } 61: 62: 63:     void OnModal(Object sender, EventArgs e) 64:     { 65:         ADialog dlg = new ADialog(); 66:         dlg.Text = "Modal"; 67:         dlg.ShowDialog(); 68:     } 69: 70:     public AnApp() 71:     { 72:         this.Size=new Size(400,100); 73:         this.BorderStyle = FormBorderStyle.FixedDialog; 74:         this.Text = "Dialog Mode Tester"; 75: 76:         Button modal = new Button(); 77:         modal.Text = "New Modal dialog"; 78:         modal.Location = new Point(10,10); 79:         modal.Size = new Size(180,25); 80:         modal.Click += new EventHandler(OnModal); 81:         this.Controls.Add(modal); 82: 83:         Button modeless = new Button(); 84:         modeless.Text = "New Modeless dialog"; 85:         modeless.Location = new Point(210,10); 86:         modeless.Size = new Size(180,25); 87:         modeless.Click += new EventHandler(OnModeless); 88:         this.Controls.Add(modeless); 89:     } 90: 91:     static void Main() 92:     { 93:     Application.Run( new AnApp()); 94:     } 95: } 

In Listing 3.2.6, the main application is a simple form with two buttons . One button instantiates a new modeless dialog. The other button instantiates a modal dialog.

Lines 70 “89 set up the main application window and tie the buttons to the click handlers on lines 52 “60 and lines 63 “68.

Lines 7 “49 define a dialog box that also has two buttons. When running from the ShowDialog command, this form will automatically put the dialog return value in the right place and close the form. When running as a modeless dialog, there is an extra setup property on lines 12 “22 that ties a click handler to the dialog buttons. Without this handler (defined on lines 24 “27), the dialog would not close in response to a button click, only by choosing the Close button on the main window bar.

Notice that you can bring up as many modeless forms as you want, but as soon as a modal dialog is shown, interaction with the main application and all the other modeless forms is blocked.

Closing the main application will close all of the modeless dialogs that are open .

Data Transfer to and from Dialog Members

In its simplest form, a dialog might only transfer data about which button was clicked to exit the dialog. A more complex dialog might make properties available to the programmer that have other information about more complex controls in the dialog. A form run with the ShowDialog method is closed by setting the DialogResult property of the form. This value is then returned to the code that invoked the dialog.

Listing 3.2.7 shows a simple dialog form with this type of behavior.

Listing 3.2.7 simpledialog.cs : A simple Dialog
 1: using System;  2: using System.Drawing;  3: using System.ComponentModel;  4: using System.Windows.Forms;  5:  6: namespace Sams {  7:  8: class SimpleDialog : Form  9: { 10:     public SimpleDialog() 11:     { 12:         // Create two buttons. 13:         Button OkButton=new Button(); 14:         OkButton.Text = "Ok"; 15:         OkButton.DialogResult = DialogResult.OK; 16:         OkButton.Location = new Point(8,20); 17:         OkButton.Size = new Size(50,24); 18:         this.Controls.Add(OkButton); 19: 20:         Button CancelButton=new Button(); 21:         CancelButton.Text = "Cancel"; 22:         CancelButton.DialogResult = DialogResult.Cancel; 23:         CancelButton.Location = new Point(64,20); 24:         CancelButton.Size = new Size(50,24); 25:         this.Controls.Add(CancelButton); 26: 27:         this.Text="Dialog"; 28:         this.Size = new Size(130,90); 29:         this.BorderStyle = FormBorderStyle.FixedDialog; 30:         this.StartPosition = FormStartPosition.CenterParent; 31:         this.ControlBox = false; 32:     } 33: } 34: 35: 36: class AnApp : Form 37: { 38:     void OnTest(Object sender, EventArgs e) 39:     { 40:         SimpleDialog dlg = new SimpleDialog(); 41:         dlg.Owner = this; 42: 43:         if(dlg.ShowDialog() == DialogResult.OK) 44:             MessageBox.Show("You clicked Ok"); 45:         else 46:             MessageBox.Show("You clicked Cancel"); 47: 48:     } 49: 50:     public AnApp() 51:     { 52:         this.Menu = new MainMenu(); 53:         this.Menu.MenuItems.Add(new MenuItem("Dialog")); 54:         this.Menu.MenuItems[0].MenuItems.Add(new MenuItem("Test")); 55:         this.Menu.MenuItems[0].MenuItems[0].Click +=             new graphics/ccc.gif EventHandler(OnTest); 56:     } 57: 58:     static void Main() 59:     { 60:         Application.Run(new AnApp()); 61:     } 62: } 63: } 

Dialogs, because they are forms, can host all the Windows Forms controls. Dialogs can be used to return simple information from check boxes, radio buttons, edit controls, and other components. To do this, you need to be able to return more than the simple dialog result seen in the previous example.

Data from dialogs can be accessed via public data members or through methods and properties that you add especially for the task. If you are a veteran MFC programmer, you might want to change your habits from the former method to the latter. Accessor properties play a big role in .NET object interaction.

The interaction between the user and the dialog can be quite complex. A button click, check box selection, or other action may generate events that are trapped by your dialog box code and acted on before the OK button is finally clicked. The design of a dialog can be as exacting as the design of the form that hosts the dialog.

Validation

Some controls, such as the NumericUpDown control, have specific behavior for validation. This control has a Minimum and Maximum property that can be used to define a numeric range for the control. The control will also fire an event when it is validating itself and again when it is validated . You can trap these events, but really the Windows Forms user interface experience is a little rough-and-ready when compared to the ease of using MFC because there is no specific DDX behavior. The validation events are inherited from System.Windows.Forms.Control and, in the case of forms shown with the ShowDialog method, are generated whenever the control loses focus. This means that validation will take place as you move from control to control within a dialog. This includes clicking a button that sets the DialogResult property and closes the dialog. An invalid condition will prevent the dialog box from closing.

Listing 3.2.8 shows a simple application that uses validation to ensure that a numeric value is correct.

Listing 3.2.8 dialogValid.cs : Retrieving validated Data from the Dialog
 1: using System;   2:  using System.ComponentModel;   3:  using System.Drawing;   4:  using System.Windows.Forms;   5:  using System.IO;   6:  using System.Text;   7:   8:  namespace Sams {   9:  10:  class DialogValid : System.Windows.Forms.Form  11:  {  12:  13:      private Button okButton;  14:      private Button cancelButton;  15:      private NumericUpDown num;  16:  17:      public decimal Num {  18:          get {  return num.Value; }  19:          set {  num.Value = value;    }  20:          }  21:  22:     void OnValidating(Object sender, CancelEventArgs e)  23:      {  24:          MessageBox.Show("NumericUpDown is validating");  25:      }  26:  27:      void OnValid(Object sender,EventArgs e)  28:      {  29:          MessageBox.Show("NumericUpDown is valid");  30:      }  31:  32:      public DialogValid()  33:      {  34:  35:          Size = new Size(400,100);  36:          FormBorderStyle = FormBorderStyle.FixedDialog;  37:          Text = "Dialog test";  38:  39:          //place the buttons on the form  40:          okButton = new Button();  41:          okButton.DialogResult = DialogResult.OK;  42:          okButton.Location = new Point(20,28);  43:          okButton.Size = new Size(80,25);  44:          okButton.Text = "OK";  45:          Controls.Add(okButton);  46:  47:          cancelButton = new Button();  48:          cancelButton.Location = new Point(300,28);  49:          cancelButton.Size = new Size(80,25);  50:          cancelButton.Text = "Cancel";  51:          cancelButton.DialogResult = DialogResult.Cancel;  52:          Controls.Add(cancelButton);  53:  54:          // place a label on the form.  55:          Label l = new Label();  56:          l.Text = "NumericUpDown";  57:          l.Location = new Point(20,5);  58:          l.Size = new Size(120,25);  59:          Controls.Add(l);  60:  61:          // finally the numeric control;  62:          num = new NumericUpDown();  63:          num.Location = new Point(140,5);  64:          num.Size = new Size(80,25);  65:          num.Minimum = (decimal)10.0;  66:          num.Maximum = (decimal)100.0;  67:          num.Value = (decimal)10.0;  68:  69:          //here we add event handlers to show the validating process.  70:          num.Validating+=new CancelEventHandler(OnValidating);  71:          num.Validated+=new EventHandler(OnValid);  72:  73:          Controls.Add(num);  74:  75:      }  76:  77:  }  78:  79:  class ddApp : System.Windows.Forms.Form  80:  {  81:  82:      void OnExit(Object sender, EventArgs e)  83:      {  84:          Application.Exit();  85:      }  86:  87:      void OnDialogTest(Object sender, EventArgs e)  88:      {  89:          DialogValid dlg = new DialogValid();  90:  91:          DialogResult r=dlg.ShowDialog();  92:  93:  94:          StringWriter sw=new StringWriter(new StringBuilder());  95:  96:          sw.WriteLine("Dialog return value = { 0} "+  97:         "\ nNumericUpDown = { 1} ",r,dlg.Num);  98:  99:          MessageBox.Show(sw.ToString()); 100: 101:      } 102: 103:      public ddApp() 104:      { 105:          MainMenu mm=new MainMenu(); 106:          mm.MenuItems.Add(new MenuItem("&Dialog")); 107:          mm.MenuItems[0].MenuItems.Add(new MenuItem("&Test", 108:             new EventHandler(OnDialogTest))); 109:          mm.MenuItems[0].MenuItems.Add(new MenuItem("-")); 110:          mm.MenuItems[0].MenuItems.Add(new MenuItem("E&xit", 111:             new EventHandler(OnExit))); 112:          Menu = mm; 113:      } 114: 115:      static void Main() 116:      { 117:          Application.Run(new ddApp()); 118:      } 119:  } 120: 121:  } 

When you derive your own controls, you can use the CausesValidation property to make the Validating and Validated events fire on your control when the focus changes within the form.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net