Vous êtes sur la page 1sur 7

NAME: S.SAVITHIRI CLASS: II.B.SC.

CS [SF]

Folder Browser Dialog: A FolderBrowserDialog control is used to browse and select a folder on a computer. A typical FolderBrowserDialog looks like Figure 1 where you can see Windows Explorer like features to navigate through folders and select a folder.

Creating a FolderBrowserDialog We can create a FolderBrowserDialog control using a Forms designer at design-time or using the FolderBrowserDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a FolderBrowserDialog does not have and not need visual properties like others. Design-time To create a FolderBrowserDialog control at design-time, you simply drag and drop a FolderBrowserDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a FolderBrowserDialog on a Form, the FolderBrowserDialog looks like Figure

Adding a FolderBrowserDialog to a Form adds following two lines of code. Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog() Run-time Creating a FolderBrowserDialog control at run-time is merely a work of creating an instance of FolderBrowserDialog class, set its properties and add FolderBrowserDialog class to the Form controls. The following code snippet shows how to use a FolderBrowserDialog control and its properties.

Private Sub BrowseButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles BrowseButton.Click Dim folderDlg As New FolderBrowserDialog folderDlg.ShowNewFolderButton = True If (folderDlg.ShowDialog() = DialogResult.OK) Then TextBox1.Text = folderDlg.SelectedPath Dim root As Environment.SpecialFolder = folderDlg.RootFolder End If End Sub A FolderBrowserDialog control allows users to launch Windows Folder Browser Dialog and let users select a folder. In this article, we discussed how to use a Windows Folder Browser Dialog and set its properties in a Windows Forms application. Color Dialog A ColorDialog control is used to select a color from available colors and also define custom colors. A typical Color Dialog looks like Figure 1 where you can see there is a list of basic solid colors and there is an option to create custom colors.

Creating a ColorDialog We can create a ColorDialog control using a Forms designer at design-time or using the ColorDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, a ColorDialog does not have and not need visual properties like others. The only purpose of ColorDialog to display available colors, create custom colors and select a color from these colors. Once a color is selected, we need that color in our code so we can apply it on other controls. Again, you can create a ColorDialog at design-time but It is easier to create a ColorDialog at runtime. Design-time

To create a ColorDialog control at design-time, you simply drag and drop a ColorDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a ColorDialog on a Form, the ColorDialog looks like Figure 2.

Adding a ColorDialog to a Form adds following two lines of code. Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog Me.ColorDialog1 = New System.Windows.Forms.ColorDialog() The following code snippet is the code for Foreground Color and Background Color buttons click event handlers. Public Class Form1 Private Sub ForegroundButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ForegroundButton.Click Dim colorDlg As New ColorDialog() colorDlg.AllowFullOpen = False colorDlg.ShowHelp = True colorDlg.Color = Color.Red colorDlg.SolidColorOnly = False If (colorDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then TextBox1.ForeColor = colorDlg.Color ListBox1.ForeColor = colorDlg.Color Button3.ForeColor = colorDlg.Color End If End Sub Private Sub BackgroundButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackgroundButton.Click Dim colorDlg As New ColorDialog() If (colorDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then TextBox1.BackColor = colorDlg.Color ListBox1.BackColor = colorDlg.Color Button3.BackColor = colorDlg.Color End If End Sub

Private Sub AddButton_Click(ByVal System.EventArgs) Handles AddButton.Click ListBox1.Items.Add(TextBox1.Text) End Sub End Class

sender

As

System.Object,

ByVal

As

A ColorDialog control allows users to launch Windows Color Dialog and let them select a solid color or create a custom color from available colors. In this article, we discussed how to use a Windows Color Dialog and set its properties in a Windows Forms application. Examples for color dialog: To display three custom colors in the lower section of the Color dialog box, use a statement such as the following: Dim colors() As Integer ColorDialog1.CustomColors = colors = {222663, 35453, 7888}

To create an array of integers that represent color values, use a statement such as the following: Dim colors() As Integer = {Math.Abs(Color.Gray.ToArgb), Math.Abs(Color.Navy.ToArgb), Math.Abs(Color.Teal.ToArgb)} The following statements set the initial color of the ColorDialog control, display the dialog box, and then use the color selected in the control to fill the form. First, place a ColorDialog control in the form and then insert the following statements in a buttons Click event handler: Private Sub Button1 Click(...) ColorDialog1.Color = If ColorDialog1.ShowDialog Windows.Forms.DialogResult.OK Me.BackColor = End End Sub Handles Button1.Click Me.BackColor = Then ColorDialog1.Color If

The following sections discuss the basic properties of the ColorDialog control.

AllowFullOpen Set this property to True if you want users to be able to open the dialog box and define their own custom colors, like the one shown in Figure 8.2. The AllowFullOpen property doesnt open the custom section of the dialog box; it simply enables the Define Custom Colors button in the dialog box. Otherwise, this button is disabled. AnyColor This property is a Boolean value that determines whether the dialog box displays all available colors in the set of basic colors. Color This is the color specified on the control. You can set it to a color value before showing the dialog box to suggest a reasonable selection. On return, read the value of the same property to find out which color was picked by the user in the control: ColorDialog1.Color If ColorDialog1.ShowDialog Me.BackColor End If CustomColors This property indicates the set of custom colors that will be shown in the dialog box. The Color dialog box has a section called Custom Colors, in which you can display 16 additional custom colors. The CustomColors property is an array of integers that represent colors. To display three custom colors in the lower section of the Color dialog box, use a statement such as the following: Dim colors() As Integer ColorDialog1.CustomColors = colors = {222663, 35453, 7888} = = = Me.BackColor DialogResult.OK Then ColorDialog1.Color

Youd expect that the CustomColors property would be an array of Color values, but its not. You cant create the array CustomColors with a statement such as this one:

Dim colors() As Color = {Color.Azure, Color.Navy, Color.Teal} Because its awkward to work with numeric values, you should convert color values to integer values by using a statement such as the following: Color.Navy.ToArgb The preceding statement returns an integer value that represents the color navy. This value, however, is negative because the first byte in the color value represents the transparency of the color. To get the value of the color, you must take the absolute value of the integer value returned by the previous expression. To create an array of integers that represent color values, use a statement such as the following: Dim colors() As Integer = {Math.Abs(Color.Gray.ToArgb), Math.Abs(Color.Navy.ToArgb), Math.Abs(Color.Teal.ToArgb)} Now you can assign the colors array to the CustomColors property of the control, and the colors will appear in the Custom Colors section of the Color dialog box. SolidColorOnly This indicates whether the dialog box will restrict users to selecting solid colors only. This setting should be used with systems that can display only 256 colors. Although today few systems cant display more than 256 colors, some interfaces are limited to this number. When you run an application through Remote Desktop, for example, only the solid colors are displayed correctly on the remote screen, regardless of the remote computers graphics card (and thats for efficiency reasons).

Vous aimerez peut-être aussi