Using Common Dialog Boxes


There are a number of dialog boxes that many programs need to use. Examples include the File Open and File Save dialog boxes, font pickers, and color choosers. Windows has always provided a set of these common dialog boxes, and they are available for you to use with Windows Forms.

Using the common dialog boxes has several advantages:

  • Programmers don’t have to keep reinventing the same dialog boxes.

  • You get fully functional dialog boxes for free, and some of them are difficult to write.

  • Users see a familiar set of dialog boxes, so they don’t have to figure out just how the File Open dialog box works in your program, for example.

  • Dialog boxes are displayed to suit the operating system. Display a File Open dialog box on a computer running Windows 2000, and you’ll see the Windows 2000 version. Do the same on a Windows XP system, and the Windows XP version will be displayed.

Windows Forms provides you with the common dialog boxes listed in the following table.

Class

Purpose

ColorDialog

Displays a dialog box to let the user pick a color.

FileDialog

The abstract base class for the OpenFileDialog and SaveFileDialog classes.

FolderBrowserDialog

Displays a dialog box that lets the user choose a folder. This class was added in .NET Framework version 1.1.

FontDialog

Displays a dialog box to let the user pick a font.

OpenFileDialog

Displays a standard File Open dialog box.

PageSetupDialog

Displays a dialog box to let the user manipulate page settings, such as margins and page orientation.

PrintDialog

Displays a dialog box to let the user select a printer and the portion of the document to print.

SaveFileDialog

Displays a standard File Save dialog box.

All these dialog boxes inherit from CommonDialog, which provides protected members that are used internally by inherited classes; you don’t use such members in your code.

These classes are used just like any other dialog box: You create a dialog box object, set its properties, and then display it by calling ShowDialog. The following exercise shows you how to use a Font dialog box to choose a font and then use it to set the font of the label control on the form.

  1. Open the CppForm project if it isn’t already open.

  2. Add a Font member to the Form1 class.

    System::Drawing::Font* labelFont;

    You have to give the fully qualified name of the Font class, or else it will conflict with the form’s Font property.

  3. Add a new item to the File menu with the text Choose Font..., call it fontDlgMenuItem, and move it above the Exit menu item. As you’ve done with other menu items, add a handler for the new menu item.

  4. The Font object is going to be used to set the font of the label on the main form. To do this, you need to create a Font object and associate it with the Font property of the label. Place the following code in the Form1 class constructor, making sure that it comes after the call to InitializeComponent:

    // Set the font labelFont = new System::Drawing::Font(S"Verdana", 16, FontStyle::Italic); label1->Font = labelFont; label1->ForeColor = Color::Black;

    Notice how the pointer to the Font is stored away in the labelFont member.

  5. Edit the handler function so that it creates and displays a FontDialog:

    private: System::Void fontDlgMenuItem_Click(System::Object * sender, System::EventArgs * e) { FontDialog* fd = new FontDialog(); fd->Font = labelFont; if (fd->ShowDialog() == DialogResult::OK) { MessageBox::Show(fd->Font->Name, S"Name was..."); labelFont = fd->Font; label1->Font = labelFont; } }

    The code creates a FontDialog object and then initializes it with the current font represented by labelFont. ShowDialog displays the dialog box, and if it returns a DialogResult::OK status, it sets the font of the label to reflect the user’s choice. The Label control will immediately update itself to use the new font.

  6. Build and run the code, and you should be able to display a Font selection common dialog box, as shown in the following figure.

    click to expand




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

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