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 _    IntegerPrivate 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 WithEnd SubPrivate 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 IfEnd SubPrivate 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_ClickEnd SubPrivate Sub Command1_Click()    ' place here the code that you want to execute    ' when the button is clickedEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may