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 |
Open a new form and give it a name, say ColorSelection_Dialog.
Select a label control from the Toolbox and draw a label on the form and set it's properties as follows :
Copy the label and then paste it back onto the form. Repeat the paste process until you have 3 rows of labels, with 16 labels in each row. Arrange the labels as the color boxes appear on the color selection dialog in the Visual Basic properties window. You should now have 48 lblColor controls on your form with index values 0 to 47.
Set the Backcolor property of each label to the corresponding color as it appears in
the VB color selection dialog.
You'll note that we only have 3 rows of colors while the VB color selection dialog has
4 with the fourth row all white. It seems a little redundant to add an extra row for
no good purpose but, of course, you could easily do so. Continue this procedure until
all of the lblColor() Backcolor properties have been set.
Add to the form a new label control. Give it a name of say ColorSelection. You can use this label to pass the value of the users color selection after the dialog has been hidden from view, so set its' Visible property to false or hide it on a part of the form that is not visible at run time.
Add the code in Listing One to the lblColor_Click event procedure.
Listing One
Sub lblColor_Click (Index As Integer)
ColorSelected = lblColor(Index).BackColor
Me.Hide
End Sub
That's it. There is no more to do. Save the form off so that you can incorporate it in your projects as required. It is up to you how you implement it.
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.)
![]()