You need the user to select a specific color for drawing.
Sample code folder: Chapter 09\UserColorSelect
For simple color-selection needs, use the ColorDialog control. This dialog, shown in Figure 9-3, lets the user select any of the 16,777,216 24-bit colors available in Windows.
Figure 9-3. The color dialog, in "full open" mode
Create a new Windows Forms application, and add the following controls to Form1:
Add informational labels if desired. The form should look something like Figure 9-4.
Figure 9-4. The controls on the color selection sample form
Now add the following source code to the form's code template:
Private Sub ActChange_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActChange.Click ' ----- Prompt to change the color. ColorSelector.Color = ColorDisplay.BackColor If (ColorSelector.ShowDialog() = _ Windows.Forms.DialogResult.OK) Then ' ----- The user selected a color. ColorDisplay.BackColor = ColorSelector.Color If (ColorSelector.Color.IsNamedColor = True) Then ' ----- Windows has a name for this color. ColorName.Text = ColorSelector.Color.Name Else ColorName.Text = "R" & ColorSelector.Color.R & _ " G" & ColorSelector.Color.G & _ " B" & ColorSelector.Color.B End If End If End Sub
Run the program, and click the Change button to access the dialog. The form will show the selected color, and either the name of the color (if known) or its red-green-blue (RGB) value.
The ColorDialog includes a few Boolean properties that let you control the availability of the "color mixer" portion of the form (the right half). The dialog does not include features that let the user indicate transparency or the "alpha" level of a color.