Standard Dialogs






Standard Dialogs

Windows Forms ships with several standard dialogs (sometimes known as "common dialogs") provided as components located in the System.Windows.Forms namespace. Here's how to manually create and show an instance of a dialog component like ColorDialog:

// ColorDialogForm.cs
partial class ColorDialogForm : Form {
  ...
  void colorDialogButton_Click(object sender, EventArgs e) {
    ColorDialog dlg = new ColorDialog();
    dlg.Color = Color.Red;
    DialogResult result = dlg.ShowDialog();
    if( result == DialogResult.OK ) {
      MessageBox.Show("You picked " + dlg.Color.ToString());
    }
  }
}

However, the common dialog components are also located on the Toolbox in VS05, and this means that you can drag them onto a form and configure them from the Properties window. This allows you to show them without writing the initialization code manually:

// ColorDialogForm.Designer.cs
partial class ColorDialogForm {
  ...
  void InitializeComponent() {
    ...
    this.colorDialog.Color = System.Drawing.Color.Red;
    ...
  }
}

// ColorDialogForm.cs
partial class ColorDialogForm : Form {
  public ColorDialogForm() {
    InitializeComponent();
  }

  void colorDialogButton_Click(object sender, EventArgs e) {
    DialogResult result = colorDialog.ShowDialog();
    if( result == DialogResult.OK ) {
      MessageBox.Show(
        "You picked " + this.colorDialog.Color.ToString());
    }
  }
}

I tend to prefer the latter approach because I like to set properties like Color visually with the Properties window, which you'll notice is also stored in InitializeComponent, thereby saving you the coding. But either approach works just fine. The following standard dialogs come with Windows Forms:[1]

[1] Neither a FindDialog nor a FindReplaceDialog exists natively in Windows Forms. However, you can download a sample implementation as part of the Genghis class library available from http://www.genghisgroup.com (http://tinysells.com/8).

  • ColorDialog allows the user to pick a color exposed by the Color property of type System.Drawing.Color (see Chapter 9).

  • FolderBrowserDialog allows the user to pick a folder exposed by the SelectedPath property of type string (see Chapter 9).

  • FontDialog allows the user to choose a font and its properties, such as bold, italics, and so on. The user-configured font object of type System.Drawing.Font is available from the Font property of the component (see Chapter 9).

  • OpenFileDialog and SaveFileDialog allow the user to pick a file to open or save, as appropriate for the dialog. The chosen file name is available from the FileName property of type string (see Chapter 9).

  • PageSetupDialog, PrintDialog, and PrintPreviewDialog are related to printing and are discussed in Chapter 8: Printing.

Excluding PrintPreviewDialog, the standard dialogs are wrappers around existing common dialogs in Windows. Because these dialogs don't support modeless operation, neither do the Windows Forms components. However, PrintPreviewDialog provides a new dialog and supports both modal and modeless operation using ShowDialog and Show, respectively.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows