VB.NET supports the Windows Common Dialogs. These dialogs are part of the Windows operating systems and are available for all applications to use. In VB.NET, they are provided as controls and include the following: -
OpenFileDialog -
SaveFileDialog -
ColorDialog -
FontDialog -
PrintDialog -
PrintPreviewDialog -
PageSetupDialog These dialogs provide part of the consistency that you find in the look and feel of all Windows applications. Each of these controls has different properties that can be used to interact with the data they collect. For example, the ColorDialog has a Color property to access the color the user selected and the FontDialog has a Font property to access the font the user selected. In addition, the ShowDialog method, which returns a DialogResult , is used to display each dialog. Using the Common Dialog Controls To use any of the common dialog controls, you must place them on your form. However, they are invisible controls and are drawn in a separate area of the form by the Windows Form Designer. The program CommonDialogExample is shown in Figure 8-26. Figure 8-26. Using the Common Dialogs. It uses the color and font dialogs to allow the user to change characteristics about the "sample text" label. The code that supports these features is found in the Click event handlers for the two buttons : Private Sub btnFont_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnFont.Click Dim answer As DialogResult answer = dlgFont.ShowDialog If answer = DialogResult.OK Then lblText.Font = dlgFont.Font End If End Sub Private Sub btnColor_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnColor.Click Dim answer As DialogResult answer = dlgColor.ShowDialog() If answer = DialogResult.OK Then lblText.BackColor = dlgColor.Color End If End Sub As you can see from Figure 8-27, the Set Font button displays the FontDialog . It displays the fonts available on the machine using a familiar dialog. When the dialog's OK button is clicked, the label's Font property is set to the font that was selected in the dialog. Figure 8-27. Using the Common Font Dialog. Figure 8-28 illustrates the ColorDialog that is displayed when the Set Color button is clicked. It displays the color palette using another familiar dialog. When the dialog's OK button is clicked, the label's BackColor property is set to the color that was selected in the dialog. Figure 8-28. Using the Common Color Dialog. |