Common Dialog Boxes

  1. The Windows provides a number of common dialog boxes that support opperations that are common among many applications
  2. It's best to use these dialogs instead of creating your own so users have less to learn when encountering new applications
  3. Color dialog box (ColorDialog)
    1. Used to select a pre-defined or custom color
      if (colorDialog1.ShowDialog() == DialogResult.OK)
      	label1.ForeColor = colorDialog1.Color;
      
      Color dialog box

  4. Font dialog box (FontDialog)
    1. Used to select font attributes
      if (fontDialog1.ShowDialog() == DialogResult.OK)
      	this.Font = fontDialog1.Font;            
      
      Font dialog box

  5. Folder browser dialog box (FolderBrowserDialog)
    1. Used to select a particular folder
      if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
      	MessageBox.Show("Selected " + folderBrowserDialog1.SelectedPath);          
      
      Folder browser dialog box

  6. Open file dialog box (OpenFileDialog)
    1. Used to select the name of an existing file
      openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files|*.*";
      if (openFileDialog1.ShowDialog() == DialogResult.OK)
          MessageBox.Show("Selected " + openFileDialog1.FileName);         
      
      Open file dialog box
    2. Note that this dialog does not really open anything!
    3. Use the filter property to specify file types
  7. Save file dialog box (SaveFileDialog)
    1. Used to supply a name to save a file
      saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files|*.*";
      if (saveFileDialog1.ShowDialog() == DialogResult.OK)
      	MessageBox.Show("Selected " + saveFileDialog1.FileName);       
      
      Save file dialog box
    2. Note that this dialog does not actually save anything!
    3. Use the filter property to specify file types
  8. Print dialog box (PrintDialog)
    1. Used to select a printer, change settings, and print a PrintDocument
      PrintDocument doc = new PrintDocument();
      doc.DocumentName = "My document";
      doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
      printDialog1.Document = doc;
      printDialog1.ShowDialog();   
      
      Save file dialog box
    2. PrintPage is an event which is triggered when the page is printing
    3. See a complete example here