ColorDialog


The ColorDialog component displays a dialog that enables the user to select a color from a standard palette or from a custom color palette. A program calls its ShowDialog method to display a color selection dialog. ShowDialog returns DialogResult.OK if the user selects a color and clicks OK. It returns DialogResult.Cancel if the user cancels. Figure G-2 shows the dialog in action.

image from book
Figure G-2: The ColorDialog component enables the user to select a color.

The following code sets the dialog box’s Color property to the btnPickColor control’s current background color. It displays the dialog box and, if the user clicks OK, it sets the btnPickColor control’s background color to the user’s selection.

  Private Sub btnPickColor_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnPickColor.Click     dlgColor.Color = btnPickColor.BackColor     If dlgColor.ShowDialog() = DialogResult.OK Then         btnPickColor.BackColor = dlgColor.Color     End If End Sub 

On the right in Figure G-2, the dialog box provides an area where the user can define custom colors. Set the component’s AllowFullOpen property to True to allow the user access this area. Set the FullOpen property to True to make the dialog appear with this area already open (otherwise the user must click the Define Custom Colors button to show this area).

If you set the SolidColorOnly property to True, the dialog box only allows the user to select solid colors. This applies only to systems using 256 or fewer colors, where some colors are dithered combinations of other colors. All colors are solid on systems using more than 256 colors.

Tip 

Dithering is the process of using dots or other shapes of various sizes to create the illusion of another color. For example, you can make orange by displaying a red area with tiny yellow dots or checks. On a system that uses a higher color model such as 24-bit color, the system can display every color directly. If you’re using a lower color model system such as 8-bit color (256 colors), the system might dither to simulate colors that it cannot display directly.

The component’s CustomColors property is an array of integers that determine the colors that the dialog displays in its custom colors area. These color values are a combination of red, green, and blue values between 0 and 255. In hexadecimal, the form of a value is BBGGRR, where BB is the blue component, GG is the green component, and RR is the red component.

For example, the color &HFF8000 has a blue component of &HFF = 255, a green component of &H80 = 128, and a red component of 0. This color is light blue. Unfortunately, the Color object’s ToArgb method returns the color in the reversed format RRGGBB, so you cannot use that method to calculate these values. Instead, you need to calculate them yourself.

The following code initializes a color dialog box’s custom colors to a range of yellows and oranges with red component 255, blue component 0, and the green component ranging from 0 to 255:

  ' Initialize the color dialog's custom colors. Dim colors() As Integer = { _       &HFF, &H11FF, &H22FF, &H33FF, _     &H44FF, &H55FF, &H66FF, &H77FF, _     &H88FF, &H99FF, &HAAFF, &HBBFF, _     &HCCFF, &HDDFF, &HEEFF, &HFFFF} dlgColor.CustomColors = colors 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net