At run time, you can set the color programmatically by
assigning it a valid known name of a color:
Public Sub InitializeComponent()
Dim dlgColor As ColorDialog = New ColorDialog()
dlgColor.Color = Color.Red
dlgColor.ShowDialog()
End Sub
When the user has finished using the Color dialog box and
clicks OK, you can find out what color was selected by retrieving the value of
the ColorDialog.Color property. Here is an example:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private pnlColored As Panel
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
Dim dlgColor As ColorDialog
pnlColored = New Panel()
pnlColored.Location = New Point(10, 10)
pnlColored.Size = New Size(200, 150)
Controls.Add(pnlColored)
dlgColor = New ColorDialog()
dlgColor.Color = Color.FromKnownColor(KnownColor.DarkTurquoise)
If dlgColor.ShowDialog = Windows.Forms.DialogResult.OK Then
pnlColored.BackColor = dlgColor.Color
End If
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
The Full View of a Color Dialog Box
|
|
By default, the Color dialog box
comes up in its regular (small) size. This allows the user to select one of the
preset colors. If the desired color is not available, as mentioned already, the
user can click the Define Custom Colors >> button. If you want to control the
user's ability to expand the dialog box, use the Boolean AllowFullOpen
property. When this property is set to True, which is its default value,
the dialog box appears in its regular size but with the Define Custom Colors >>
button enabled. If you want the user to be able to select only one of the preset
colors and not have the ability to expand the dialog box, set the
AllowFullOpen property to False. Here is an example:
Public Sub InitializeComponent()
Dim dlgColor As ColorDialog
dlgColor = New ColorDialog()
dlgColor.AllowFullOpen = False
dlgColor.ShowDialog()
End Sub
With this value, when the Color dialog box comes up, it is
in its regular size but the Define Custom Colors >> button is disabled:
As mentioned already, by default, the Color dialog box
displays in its regular size. You can control the regular or full size of the
dialog using the Boolean FullOpen property. When its value is False,
which is the default, the dialog appears regularly. If you want it to appear in
its full size, set this property to True.
|