Windows Forms includes a host of common dialog classes, enabling a consistent and easy-to-operate user interface. The common dialog classes inherit the System.Windows.Forms.CommonDialog class. CommonDialog implements many of the methods that the common dialogs use, which contributes to the consistency and ease of use among all the common dialogs. This section covers the common dialogs and provides tips on how to use them effectively. To create each of the listings in this section, I dragged a Button control to the form and added relevant code to its Click event. All of the dialogs can be dragged from the Tool Palette and dropped onto the design surface. They reside toward the bottom of the form designer (see Figure 7.1), as do other nonvisual components. Figure 7.1. A common dialog in the form designer.
ColorDialogThe ColorDialog offers a common way to obtain a Color object for applications. Listing 7.6 shows how to use it. Listing 7.6 The ColorDialog Class (ColorDlg.cs)private void btnColor_Click(object sender, System.EventArgs e) { colorDialog1.ShowDialog(); MessageBox.Show(colorDialog1.Color.ToString()); } As shown in Listing 7.6, the only requirement for displaying a ColorDialog is to execute its ShowDialog method. It uses the Color property to extract which color the user selected. ColorDialog has some properties that control how it is displayed (see Table 7.3). Each property is available for setting in the Object Inspector.
FolderBrowserDialogThe FolderBrowserDialog is used for selecting a directory and optionally creating new directories. It can be set to begin display at various places in a system. Listing 7.7 shows how to use the FolderBrowserDialog. Listing 7.7 The FolderBrowserDialog Class (FolderDlg.cs)private void btnFolder_Click(object sender, System.EventArgs e) { folderBrowserDialog1.ShowDialog(); MessageBox.Show(folderBrowserDialog1.SelectedPath); } The routine in Listing 7.7 displays the FolderBrowserDialog with the ShowDialog method. The SelectedPath property returns the path that the user selected. If no path was selected, the path for the RootFolder property will be returned. The FolderBrowserDialog exposes properties that can be altered in the Object Inspector (see Table 7.4).
FontDialogThe FontDialog displays common fonts, the same way that a word processor or any text editing application does. Listing 7.8 shows how to use this component. Listing 7.8 The FontDialog Class (FontDlg.cs)private void btnFont_Click(object sender, System.EventArgs e) { fontDialog1.ShowDialog(); MessageBox.Show(fontDialog1.Font.ToString()); } Listing 7.8 shows how to start a FontDialog. After a font has been selected, a Font object can be obtained from the Font property. Table 7.5 describes the FontDialog properties that can be set or read from. These properties can be set in the Object Inspector.
OpenFileDialogAn OpenFileDialog lets a user navigate to and open files. Listing 7.9 shows how to use the OpenFileDialog. Listing 7.9 The OpenFileDialog Class (OpenFileDlg.cs)private void btnOpen_Click(object sender, System.EventArgs e) { openFileDialog1.ShowDialog(); MessageBox.Show(openFileDialog1.FileName); } The code in Listing 7.9 uses the FileName property to extract the name of a single selected file from the OpenFileDialog. If the MultiSelect property of the OpenFileDialog is set to true, multiple files can be obtained by reading the FileNames property, which is an array of strings with the name of each file the user selected. Table 7.6 shows the properties of the OpenFileDialog that can be set in the Object Inspector.
SaveFileDialogThe SaveFileDialog enables users to save a file. Listing 7.10 shows how to use this dialog. Listing 7.10 The SaveFileDialog Class (SaveFileDlg.cs)private void btnSave_Click(object sender, System.EventArgs e) { saveFileDialog1.ShowDialog(); MessageBox.Show(saveFileDialog1.FileName); } Listing 7.10 shows how to implement a SaveFileDialog. It retrieves the name that the file was saved as by reading the FileName property. Table 7.7 shows the SaveFileDialog properties that can be set in the Object Inspector.
PageSetupDialogThe PageSetupDialog has options for configuring page layout and printer settings. Listing 7.11 shows how to use it. Listing 7.11 The PageSetupDialog Class (PageSetupDlg.cs)private void btnPageSetup_Click(object sender, System.EventArgs e) { pageSetupDialog1.Document = new PrintDocument(); pageSetupDialog1.ShowDialog(); string settings = pageSetupDialog1.PageSettings.ToString() + "\n\n" + pageSetupDialog1.PrinterSettings.ToString(); MessageBox.Show(settings); } In Listing 7.11, the Document property of the PageSetupDialog is set to a default instance of the PrintDocument class. This is the preferred method of initializing the PageSetupDialog. Whatever PrintDocument object you pass to the Document property can be customized as needed. Table 7.8 shows which properties of the Object Inspector can be set to customize the PageSetupDialog. Remember to add a using declaration for System.Drawing.Printing, the namespace for PrintDocument, to the top of the file. Otherwise, you'll need to fully qualify the type as System.Drawing.Printing.PrintDocument.
PrintPreviewDialogThe PrintPreviewDialog enables a user to view a preview of a page as it would look if printed. Listing 7.12 shows how to use the PrintPreviewDialog. Listing 7.12 The PrintPreviewDialog Class (PrintPreviewDlg.cs)private void btnPrint_Click(object sender, System.EventArgs e) { PrintDocument myPrintDoc = new PrintDocument(); myPrintDoc.PrintPage += new PrintPageEventHandler(PagePrinter); printPreviewDialog1.Document = myPrintDoc; printPreviewDialog1.ShowDialog(); } private void PagePrinter(object sender, PrintPageEventArgs ev) { int x = ev.MarginBounds.Width/2; int y = ev.MarginBounds.Height/2; ev.Graphics.DrawString( "Welcome to C#Builder Kick Start", Font, Brushes.Black, x, y, new StringFormat()); ev.HasMorePages = false; } The PrintPreviewDialog in Listing 7.12 is set with a PrintDocument object and then displayed, similar to the PageSetupDialog. The difference this time is that the PrintDocument object was actually initialized with more than default values. If you set the PrintPage event of the PrintDocument object with a delegate that references the PagePrinter method as its parameter, the code has full control over what is displayed in the PrintPreviewDialog. Everything used in the PagePrinter routine uses some property of the PrintPageEventArgs parameter. Because the routine centers a message on the screen, it obtains the Height and Width properties of the MarginBounds property of the PrintPageEventArgs. Dividing the Height and Width properties of the MarginBounds by two gets the center point on the screen. In the DrawString method, we specify the string, the inherited form's Font property, the Black brush from the Brushes enum, the x and y coordinates of where to write the string, and a default StringFormat object. As long as there are pages in a document to print, HasMorePages would be set to true. However, this routine just wants to render a single page, so it sets HasMorePages to false. Besides the Document property, shown in Listing 7.12, the PrintPreviewDialog has a UseAntiAlias property, which helps to smooth font output on some printers. PrintDialogThe PrintDialog enables a user to print a document. Listing 7.13 shows how to use the PrintDialog. Listing 7.13 The PrintDialog Class (PrintDlg.cs)private void btnPrint_Click(object sender, System.EventArgs e) { printDialog1.Document = new PrintDocument(); printDialog1.ShowDialog(); } To make the PrintDialog work, you must set its Document property with a new PrintDocument object, as shown in Listing 7.13. See Listing 7.12 for how to add an event handler to the PrintDocument that controls page printing. |