Make A Pop Up Color Selector

With the Common Dialog Control you can easily add a pop up color selector to your application.
The following code will pop up color selector, and will paint the form with the selected color.

Preparations

Add 1 Common Dialog Control to your form (From VB menu choose Project->Components..., then mark the Microsoft Common Dialog Control check box, and press OK. Now drag the new control to your form).

Form Code

Private Sub Command1_Click()
' The following line says: if the user will press the cancel Button, 
' treat it like if an error occurred in the program.

    CommonDialog1.CancelError = True
' If an error occurred in the program, jump to the
' CancelPressed part of the program, below. 

    On Error GoTo CancelPressed
' Pop up the Color Selector
    CommonDialog1.ShowColor
' Paint the from with the chosen color.
' CommonDialog1.Color holds the color that the user has selected.
   
Form1.BackColor = CommonDialog1.Color
' exit the Command1_Click() sub
    Exit Sub
CancelPressed:
' If the user pressed the cancel button an error was occurred, and
' the program had jumped to here.
' if the user didn't press the cancel button, the program doesn't apply this
' lines because it already has exited this sub from the "Exit Sub" line above

    MsgBox "You pressed Cancel"
End Sub

Go Back