Create colorful Command Buttons

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as