Home

Introduction

 

Fundamentals

 

Introduction

 

 

 
   
 
 

 

 
 
   
 

Previous Copyright © 2015 FunctionX Next

Home

The Color Dialog Box

 

Fundamentals of the Color Dialog Box

   

Introduction

To provide the selection of colors on Microsoft Windows applications, the operating system provides a common dialog box appropriate for such tasks. You can use the Color dialog box for various reasons such as letting the user set or change a color of an object or specifying the background color of a control or the color used to paint an object. When it displays, by default, the dialog box appears as follows:

The Color Dialog Box

This displays a constant list of colors to the user. If none of the available colors is appropriate for the task at hand, the user can click the Define Custom Colors >> button to expand the dialog box:

The Expanded Dialog Box

The expanded Color dialog box allows the user either to select one of the preset colors or to custom create a color by specifying its red, green, and blue values.

The user can change the color in four different areas. The top left section displays a list of 48 predefined colors. If the desired color is not in that section, the user can click and drag the mouse in the multi-colored palette. The user can also drag the right bar that displays a range based on the color of the palette; the user can scroll up and down by dragging the arrow. For more precision, the user can type the Red, Green and Blue values. Each uses an integral value that ranges from 1 to 255.

Making a Color Dialog Box Available

The Color dialog box is implemented through the ColorDialog class, which is based on the CommonDialog class that is the ancestor to all Windows common dialog boxes of the .NET Framework. To display the dialog box to the user, call the CommonDialog.ShowDialog() method. Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise = new Form(Text = "Exercise",
                        StartPosition = FormStartPosition.CenterScreen,
                        ClientSize = new System.Drawing.Size(316, 260))

let dlg : ColorDialog = new ColorDialog()

let btnColor = new Button(Text = "Color",
                          Location = new Point(12, 12),
                          FlatStyle  = FlatStyle.Popup,
                          Size = new System.Drawing.Size(140, 100))
                          
let btnColorClick (e : EventArgs) =
    if dlg.ShowDialog() = DialogResult.OK then ()
btnColor.Click.Add btnColorClick

exercise.Controls.Add btnColor
        
let btnClose = new Button(Text = "Close",
                          Location = new Point(228, 226))

let btnCloseClick (e : EventArgs) =
    exercise. Close()
btnClose.Click.Add btnCloseClick
exercise.Controls.Add btnClose        

[<EntryPoint>]
[<STAThread>]
let main argv =
    Application.Run exercise
    0

The Characteristics of the Color Dialog Box

 

The Color Produced by a Color Dialog Box

The most important and most obvious property of the Color dialog box is the color the user would have selected after using it. This selected color is represented by the ColorDialog.Color property. At run time, you can set the color programmatically by assigning it a valid known name of a color:

open System
open System.Drawing
open System.Windows.Forms

let exercise = new Form(Text = "Exercise",
                        StartPosition = FormStartPosition.CenterScreen,
                        ClientSize = new System.Drawing.Size(316, 260))

let dlg : ColorDialog = new ColorDialog()
dlg.Color <- Color.Red

let btnColor = new Button(Text = "Color",
                          Location = new Point(12, 12),
                          FlatStyle  = FlatStyle.Popup,
                          Size = new System.Drawing.Size(140, 100))
                          
let btnColorClick (e : EventArgs) =
    if dlg.ShowDialog() = DialogResult.OK then ()
btnColor.Click.Add btnColorClick

exercise.Controls.Add btnColor
        
let btnClose = new Button(Text = "Close",
                          Location = new Point(228, 226))

let btnCloseClick (e : EventArgs) =
    exercise. Close()
btnClose.Click.Add btnCloseClick
exercise.Controls.Add btnClose        

[<EntryPoint>]
[<STAThread>]
let main argv =
    Application.Run exercise
    0

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:

open System
open System.Drawing
open System.Windows.Forms

let exercise = new Form(Text = "Exercise",
                        StartPosition = FormStartPosition.CenterScreen,
                        ClientSize = new System.Drawing.Size(316, 260))

let dlg : ColorDialog = new ColorDialog()
dlg.Color <- Color.Red

let btnColor = new Button(Text = "Color",
                          Location = new Point(12, 12),
                          FlatStyle  = FlatStyle.Popup)

let pnlColor = new Panel(Location = new Point(12, 42),
                          BorderStyle  = BorderStyle.Fixed3D,
                          Size = new System.Drawing.Size(288, 175))
exercise.Controls.Add pnlColor

let btnColorClick (e : EventArgs) =
    if dlg.ShowDialog() = DialogResult.OK then
        pnlColor.BackColor <- dlg.Color
btnColor.Click.Add btnColorClick

exercise.Controls.Add btnColor
        
let btnClose = new Button(Text = "Close",
                          Location = new Point(228, 226))

let btnCloseClick (e : EventArgs) =
    exercise. Close()
btnClose.Click.Add btnCloseClick
exercise.Controls.Add btnClose        

[<EntryPoint>]
[<STAThread>]
let main argv =
    Application.Run exercise
    0

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. With this value, when the Color dialog box comes up, it is in its regular size but the Define Custom Colors >> button is disabled:

The Color dialog box that cannot be expanded

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.


Home Copyright © 2015, FunctionX Home