Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Oct 30, 1999

Create colorful Command Buttons

The VB CommandButton control supports neither the ForeColor nor the BackColor properties. If you want to create colorful buttons without resorting to 3rd party controls, you can use an OptionButton control with the Style property set to 1-Graphical. Such OptionButton controls appear to be very similar to regular CommandButton controls, but they let you modify their foreground and background colors. To have them behave as CommandButton controls you only have to add the following line of code in their Click event procedure:


Private Sub Option1_Click()
    ' reset the value to False, so that the
    ' control doesn't appear to be depressed
    Option1.Value = False
    ' here you insert the code that you want
    ' to execute when the button is clicked
    ' ....
End Sub
One problem with this simple approach is that the border of this "fake" CommandButton control doesn't change its appearance when the control has the focus. Another problem is that you can't easily make this button the default button on the form, unless you trap the Enter key with a form-level keyboard handler (that is, you set KeyPreview = True).

You can solve both problems using a real CommandButton control and place it "behind" the colorful OptionButton control. This will provide the desidered border (without having to draw it yourself) and if the Default property of such a button is set to Default, it will work as expected.

The following code excerpt shows how you can do the trick. It assumes that Option1 is the colorful OptionButton control, and Command1 is the hidden CommandButton control:


Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As _
    Integer

Private Sub Form_Load()
    With Option1
        ' move the CommandButton control under the OptionButton control,
        Command1.Move .Left, .Top, .Width, .Height
        ' but shrink the latter control, so that the border is visible
        .Move .Left + ScaleX(1, vbPixels, ScaleMode), .Top + ScaleY(1, vbPixels, _
            ScaleMode), .Width - 2 * ScaleX(1, vbPixels, ScaleMode), _
            .Height - 2 * ScaleY(1, vbPixels, ScaleMode)
        ' ensure that the OptionButton is on top
        .ZOrder
        ' the user can't tab to the OptionButton
        .TabStop = False
    End With
End Sub

Private Sub Option1_Click()
    If GetAsyncKeyState(vbKeyLButton) = 0 And Option1.Value Then
        ' if the optionbutton is activated using a hotkey
        ' or the user clicked on it and then released the mouse
        ' we must reset its appearance
        Option1.Value = False
        ' and then fire the Click event of the real button
        Command1.SetFocus
        Command1.Value = True
    End If
End Sub

Private Sub Option1_MouseUp(Button As Integer, Shift As Integer, X As Single, _
    Y As Single)
    ' fire the Click event again when the mouse button is released
    Option1_Click
End Sub

Private Sub Command1_Click()
    ' place here the code that you want to execute
    ' when the button is clicked
End Sub


Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date