How to Make a Color Selection Dialog Box

Follow These Easy Steps...

Property Value
BackStyle Opaque
Caption Up to you
Name lblColor
Index 0
Height 300
Width 300

You might call the dialog from a menu option as per my simple example in Listing Two. (Note that you don't have to explicitly refer to the labels caption property when retrieving or passing its value. This can make your code very English like and easy to read.)


  Listing Two
  Sub mnuSelectColor_Click()                      ' Get the user's color selections for the currently loaded form.
    ColorSelection_Dialog.Show
    Me.BackColor = ColorDialog.ColorSelected
  End Sub

The finished form occupies just under 15K of disk space while an exact copy that used picturebox controls instead of label controls occupied over 22K of disk space. Both versions would occupy considerably less space once compiled into a Visual Basic project.

This example demonstrates how the humble label control can be used to achieve what at first may seem a difficult task, yet the solution offered relies on the manipulation of a controls' properties at design time with very little code behind the scenes.

You could, of course, just use the common dialog control that comes with Visual Basic but then you would have to always include the CMDIALOG.VBX control in your installation and setup-up programs.

You can also adapt this technique to your specific needs where CMDIALOG.VBX just wouldn't be appropriate.

For example, if you designed a paint program similar to good old reliable PaintBrush you would need the color selection palette to be visible on the main window of the application - something CMDIALOG isn't going to manage.

With this technique, you could just select all of the labels from the ColorSelection_Dialog, copy them and then paste them into the form where you wanted them to be.

You might also want to extend the selection if your application was intended to run in a higher colour resolution... you can even allow user-defined colours with a bit of extra work. It's a far more flexible approach.

VB4 Note : By using the label control to create your colors pallet you will be able to copy and paste the controls collections between different version of MS Visual Basic. This is important because VB3 controls are not compatible with the VB4 IDE.

(Of course if you are using my application Object Library(Pro) you could store the labels in a library then drag and drop them into forms as required without having to search for them in a form.)


[HOME] [TABLE OF CONTENTS] [SEARCH]