9.17 Show the Common File Dialogs


Problem

You need to show the standard Windows dialog boxes for opening and saving files and for selecting a folder.

Solution

Use the OpenFileDialog , SaveFileDialog , and FolderBrowserDialog classes in the System.Windows.Forms namespace. Call the ShowDialog method to display the dialog, examine the return value to determine whether the user clicked OK or Cancel, and retrieve the selection from the FileName or SelectedPath property.

Discussion

.NET provides objects that wrap many of the standard Windows dialog boxes, including those used for saving and selecting files and directories. These classes all inherit from System.Windows.Forms.CommonDialog and include the following:

  • OpenFileDialog , which allows the user to select a file. The file name and path is provided to your code through the FileName property (or the FileNames collection, if you have enabled multiple file select by setting Multiselect to true ). Additionally, you can use the Filter property to set the file format choices and set CheckFileExists to enforce validation. (See Figure 9.2.)

  • SaveFileDialog , which allows the user to specify a new file. The file name and path is provided to your code through the FileName property. You can also use the Filter property to set the file format choices and set the CreatePrompt and OverwritePrompt Boolean properties to instruct .NET to display a confirmation if the user selects a new file or an existing file, respectively.

  • FolderBrowser, which allows the user to select (and optionally create) a directory. The selected path is provided through the SelectedPath property, and you can specify whether or not a Create New button should appear. (See Figure 9.3.)

    click to expand
    Figure 9.2: The OpenFileDialog .


    Figure 9.3: The FolderBrowserDialog .

When using OpenFileDialog or SaveFileDialog , you need to set the filter string, which specifies the allowed file extensions. The filter string is separated with the pipe character () in this format: "[Text label] [Extension list separated by semicolons] [Text label] [Extension list separated by semicolons] . . ." You can also set the Title (form caption) and the IntialDirectory .

The following code shows a Windows-based application that allows the user to load documents into a RichTextBox , edit the content, and then save the modified document. When opening and saving a document, the OpenFileDialog and SaveFileDialog classes are used.

 using System; using System.Drawing; using System.Windows.Forms; public class SimpleEditForm : System.Windows.Forms.Form {     private System.Windows.Forms.MenuItem mnuFile;     private System.Windows.Forms.MenuItem mnuOpen;     private System.Windows.Forms.MenuItem mnuSave;     private System.Windows.Forms.MenuItem mnuExit;     private System.Windows.Forms.RichTextBox rtDoc;     // (Designer code omitted.)     private void mnuOpen_Click(object sender, System.EventArgs e) {         OpenFileDialog dlg = new OpenFileDialog();         dlg.Filter = "Rich Text Files (*.rtf)*.RTF" +           "All files (*.*)*.*";         dlg.CheckFileExists = true;         dlg.InitialDirectory = Application.StartupPath;         if (dlg.ShowDialog() == DialogResult.OK) {             rtDoc.LoadFile(dlg.FileName);             rtDoc.Enabled = true;         }     }     private void mnuSave_Click(object sender, System.EventArgs e) {         SaveFileDialog dlg = new SaveFileDialog();         dlg.Filter = "RichText Files (*.rtf)*.RTFText Files (*.txt)*.TXT" +           "All files (*.*)*.*";         dlg.CheckFileExists = true;         dlg.InitialDirectory = Application.StartupPath;         if (dlg.ShowDialog() == DialogResult.OK) {             rtDoc.SaveFile(dlg.FileName);         }     }     private void mnuExit_Click(object sender, System.EventArgs e) {         this.Close();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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