ColorDialog FontDialog OpenFileDialog Common Dialog Boxes

211

5.4.1 ColorDialog

The ColorDialog component displays a dialog box that allows the user to choose a color. After the user clicks OK, the chosen color is available in the ColorDialog objects Color property. The Color property can also be set prior to showing the dialog box. This causes the dialog box to initially display the given color. Figur e 5- 14 shows an example of the ColorDialog dialog box. Figure 5-14. The ColorDialog dialog box

5.4.2 FontDialog

The FontDialog component displays a dialog box that allows the user to choose a font. After the user clicks OK, the chosen font is available in the FontDialog objects Font property. The Font property can also be set prior to showing the dialog box. This causes the dialog box to initially display the given font. Figur e 5- 15 shows an example of the FontDialog dialog box. Figure 5-15. The FontDialog dialog box 212

5.4.3 OpenFileDialog

The OpenFileDialog component displays a dialog box that allows the user to choose a file to open. After the user clicks OK, the name of the file including the path is available in the OpenFileDialog objects FileName property. The FileName property can be set prior to showing the dialog box. This causes the dialog box to initially display the given filename. Figur e 5- 16 shows an example of the OpenFileDialog dialog box. Figure 5-16. The OpenFileDialog dialog box In most cases, your applications should set the InitialDirectory, Filter, and FilterIndex properties prior to calling ShowDialog. This is not necessary for proper functioning of the dialog box, but it will give your application a more professional look and feel. The InitialDirectory property determines which directory is shown when the dialog box first appears. The default is an empty string, which causes the dialog box to display the users My Documents directory. The Filter property holds a String value that controls the choices in the Files of type drop-down list. The purpose of this drop-down list is to let the user limit the files shown in the dialog box based on filename extension. A typical example is shown in Figur e 5- 17 . Figure 5-17. A typical Files of type drop-down list Even though the Files of type list can include many items and each item can represent many filename extensions, a single String property represents the whole thing. Heres how it works: • Each item in the drop-down list is represented by a substring having two parts separated by the vertical bar character | . The first part is the description that appears in the drop-down list e.g., All Files . . The second part is the corresponding filter e.g., . . Taking them together and adding the vertical bar character, the first item in the list in Figur e 5- 17 is represented by the substring: All Files .|. 213 • If a given item has multiple filters, the filters are separated by semicolons ; . The second item in the list in Figur e 5- 17 is therefore represented by: Executable Files .exe; .dll|.exe;.dll • The value to assign to the Filter property is the concatenation of all the substrings thus attained, again separated by the vertical bar character. Therefore, the Filter property value that produced the drop-down list in Figur e 5- 17 is: All Files .|.|Executable Files .exe; .dll|.exe;.dll The default value of the Filter property is an empty string, which results in an empty Files of type drop-down list. The FilterIndex property determines which filter is in force when the dialog box is initially shown. This is a 1-based index that refers to the Filter string. For example, referring again to Figur e 5- 17 , if the FilterIndex property is set to 1 , the All Files item will be selected when the dialog box is shown. If the FilterIndex is set to 2 , the Executable Files item will be shown. The default value is 1 .

5.4.4 PageSetupDialog