Common Dialogs

Since Windows 3.1, the operating system itself has provided common dialogs for use by applications to keep a consistent look and feel. WPF has three intrinsic dialogs that map to those provided by Windows—OpenFileDialog, SaveFileDialog, and PrintDialog—and they work the way you'd expect them to.* Example 10-4 shows the OpenFileDialog in action.

Example 10-4. Using the OpenFileDialog common dialog using Microsoft.Win32; // home of OpenFileDialog and SaveFileDialog using System.Windows.Controls; // home of PrintDialog string filename;

void openFileDialogButton_Click(...) { OpenFileDialog dlg = new OpenFileDialog(); dlg.FileName = filename;

if (dlg.ShowDialog() == true) { filename = dlg.FileName;

If you'd like access to other common Windows Forms dialogs, like the folder browser dialog, you can bring in the System.Windows.Forms assembly^ and use them without much trouble,* as shown in Example 10-5.

Example 10-5. Showing the FolderBrowserDialog common Windows Forms dialog string folder;

void folderBrowserDialogButton_Click(...) {

* Unlike the common dialogs provided by Windows Forms, the WPF ShowDialog returns a nullable Boolean value, not an enumerated value. We'll talk more about that later.

t Of course, now you're paying the cost of loading an additional assembly into your process, but that beats reimplementing the Windows Forms common dialogs in many scenarios.

* If you'd like to take advantage of new Vista features in your common dialog usage, you can do so with samples provided in the SDK in the topic titled "Vista Bridge Samples," available at http://msdn2.microsoft.com/ en-us/library/ms756482.aspx (http://tinysells.com/95).

Example 10-5. Showing the FolderBrowserDialog common Windows Forms dialog (continued)

System.Windows.Forms.FolderBrowserDialog dlg =

new System.Windows.Forms.FolderBrowserDialog(); dlg.SelectedPath = folder;

if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) { folder = dlg.SelectedPath;

// do something with the folder...

In Example 10-5, we're creating an instance of the FolderBrowserDialog class from the System.Windows.Forms namespace. The fully qualified type name is useful instead of the typical using statement at the top of the file so that we don't collide with any of the WPF types and thereby require those to be fully qualified, too.

After creation, the code in Example 10-5 calls the ShowDialog method, which shows the dialog modally until the dialog has closed. When that happens, ShowDialog returns with a result—generally, OK or Cancel (not true or false, like WPF dialogs). When it's OK, we can use the properties of the dialog set by the user.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment