devxlogo

Change a CheckBox or OptionButton style at runtime

Change a CheckBox or OptionButton style at runtime

Visual Basic doesn’t let you change the Style property of a CheckBox or an OptionButton control at runtime. However, you can easily do it by manipulating the control’s style bit, with the SetWindowLong API function. Here’s a routine that does the trick:

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _    (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal newValue As Long) As LongConst GWL_STYLE = (-16)Const BS_PUSHLIKE = &H1000&Sub SetButtonStyle(Ctrl As Control, ByVal Graphical As Boolean)    If Graphical Then        SetWindowLong Ctrl.hWnd, GWL_STYLE, GetWindowLong(Ctrl.hWnd, _            GWL_STYLE) Or BS_PUSHLIKE    Else        SetWindowLong Ctrl.hWnd, GWL_STYLE, GetWindowLong(Ctrl.hWnd, _            GWL_STYLE) And Not BS_PUSHLIKE    End If    Ctrl.RefreshEnd Sub

Using the routine is straightforward. For example, this code changes the style of all the OptionButton controls in the Option1 array

    Dim c As Control    For Each c In Option1        SetButtonStyle c, True    Next

See also  5 Tips for Choosing the Right Medical Software for Your Practice
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist