The last dialogs you look at in this chapter are the FontDialog and the ColorDialog.
Note | This chapter discusses these dialogs for setting font and color the Font and Color classes are covered in Chapter 30. |
The FontDialog lets the user choose a font. The user can change the font, style, size, and color of the font.
Figure 16-29 shows the properties that change the behavior of the elements in the dialog.
Figure 16-29
The dialog can be used in the same way as the previous dialogs. In the Windows Forms designer the dialog can be dragged from the Toolbox and dropped to the form so that an instance of the FontDialog gets created.
The code to use the FontDialog can look like this:
if (dlgFont.ShowDialog() == DialogResult.OK) { textBoxEdit.Font = dlgFont.Font; }
The FontDialog is displayed by calling the ShowDialog() method. If the user presses the OK button, DialogResult.OK is returned from the method. The selected font can be read by using the Font property of the FontDialog class; this font is then passed to the Font property of the TextBox.
You have already seen a picture with properties of the FontDialog class; but now let's see what these properties are used for.
Property | Description |
---|---|
AllowVectorFonts | Boolean value that defines if vector fonts can be selected in the font list. The default is true. |
AllowVerticalFonts | Boolean value that defines if vertical fonts can be selected in the font list. Vertical texts are used in far eastern countries. There probably isn't a vertical font installed on your system. The default is true. |
FixedPitchOnly | Setting the property FixedPitchOnly displays only fixed pitch fonts in the font list. With a fixed pitch font every character has the same size. The default is false. |
MaxSize | Specifying a value for the MaxSize property defines the maximum font size the user can select. |
MinSize | Similar to MaxSize you can set the minimum font size the user can select with MinSize. |
ShowApply | If the Apply button should be displayed you have to set the ShowApply property to true. By pressing the Apply button the user can see an updated font in the application without leaving the font dialog. |
ShowColor | By default the Color selection is not shown in the dialog. If you want the user to select the font color in the font dialog you just have to set the ShowColor property to true. |
ShowEffects | By default the user can select the Strikeout and Underline check boxes to manipulate the font. If you don't want these options to be displayed you have to set the ShowEffects property to false. |
AllowScriptChange | Setting the AllowScriptChange property to false prevents the user from changing the script of a font. The available scripts depend on the selected font. For example, the font Arial supports Western, Hebrew, Arabic, Greek, Turkish, Baltic, Central European, Cyrillic, Vietnamese scripts. |
An interesting difference from the other dialogs presented so far is that the FontDialog supports an Apply button, which is not displayed by default. If the user presses the Apply button the dialog stays opened, but the font should be applied.
By selecting the FontDialog in the Windows Forms designer you can set the ShowApply property in the Properties window to true. But how are you informed if the user now presses the Apply button? The dialog is still opened, so the ShowDialog() method will not return. Instead, you can add an event handler to the Apply event of the FontDialog class. You can do this by pressing the Events button in the Properties window, and by writing a handler name to the Apply event.
As you can see in the following code, I have entered the name OnApplyFontDialog. In this handler, you can access the selected font of the FontDialog using the member variable of the FontDialog class:
private void OnApplyFontDialog(object sender, System.EventArgs e) { textBoxEdit.Font = dlgFont.Font; }
There isn't as much to configure in the ColorDialog as for the FontDialog. The ColorDialog makes it possible to configure custom colors if none of the basic colors are wanted. This is done by setting the AllowFullOpen property. The custom color configuration part of the dialog can also be automatically expanded with the FullOpen property. If AllowFullOpen is false, then the value of FullOpen will be ignored. the SolidColorOnly property specifies that only solid colors can be selected. the CustomColors property can be used to get and set the configured custom color values.
Figure 16-30 shows the color dialog with the properties that influence the dialog.
Figure 16-30
The ColorDialog can be dragged from the Toolbox and dropped onto the form in the Windows Forms designer, as you have done with the other dialogs. ShowDialog() displays the dialog until the user presses the OK or Cancel button. You can read the selected color by accessing the Color property of the dialog, as can be seen in the following code example:
if (dlgColor.ShowDialog() == DialogResult.OK) { textBoxEdit.ForeColor = dlgColor.Color; }
The properties to influence the look of the dialog are summarized in this table:
Properties | Description |
---|---|
AllowFullOpen | Setting this property to false disables the Define Custom Colors button, thus preventing the user from defining custom colors. The default value of this property is true. |
FullOpen | Setting the FullOpen property to true before the dialog is displayed opens the dialog with the custom color selection automatically displayed. |
AnyColor | Setting this property to true shows all available colors in the list of basic colors. |
CustomColors | With the CustomColors property you can preset an array of custom colors, and you can read the custom colors defined by the user. |
SolidColorOnly | By setting the SolidColorOnly property to true the user can only select solid colors. |
A new dialog with .NET 1.1 is a simple dialog to get directory names from the user or to create directories. The class of this dialog is FolderBrowserDialog. Just a few properties are available to configure this dialog as you can see in Figure 16-31.
Figure 16-31
The Description can be used to define the text above the tree view. RootFolder defines the folder where the user's search of the folder to select should start. With the RootFolder property, you can set a value from the enumeration Environment.SpecialFolder. ShowNewFolderButton defines if the user is allowed to create a new folder with the dialog.
The FolderBrowserDialog can be dragged from the Toolbox and dropped onto the form in the Windows Forms designer, as you have done with the other dialogs. ShowDialog() displays the dialog until the user presses the OK or Cancel button. You can read the path that is selected by the user using the SelectedPath property, as can be seen in the following code example:
dlgFolderBrowser.Description = "Select a directory"; if (dlgFolderBrowser.ShowDialog() == DialogResult.OK) { MessageBox.Show("The folder " + dlgFolderBrowser.SelectedPath + " was selected"); }
The properties to influence the behavior of the dialog are summarized in the following table.
Properties | Description |
---|---|
Description | With the Description property you can define the text that shows up above the tree view of the dialog. |
RootFolder | With the RootFolder property you can set the path where the browsing should start. |
SelectedPath | The property SelectedPath returns the path of the directory that is selected by the user. |
ShowNewFolderButton | By setting the ShowNewFolderButton to true, the user can create a new folder. |