Make your own free website on Tripod.com
Set Option Button And Check Box Style Property At Runtime

If you ever tried to set Option Button style property at runtime, you got "can't assign to read-only property" error message. To solve this problem you can use this code, that works also with Check Box.

Preparations

Add 1 Option Button and 2 Command Buttons to your form.

Module Code

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_STYLE = (-16)
Public Const BS_PUSHLIKE = &H1000&
 

Public Sub SetGraphicStyle(StyleButton As Control, Flag As Boolean)
    Dim curstyle As Long
    Dim newstyle As Long
 
'exit this sub if the control is not Option Button or Check Box
   
If Not TypeOf StyleButton Is OptionButton And _
    Not TypeOf StyleButton Is CheckBox Then Exit Sub
 
    curstyle = GetWindowLong(StyleButton.hwnd, GWL_STYLE)
 
    If Flag Then
        curstyle = curstyle Or BS_PUSHLIKE
    Else
        curstyle = curstyle And (Not BS_PUSHLIKE)
    End If
 
    newstyle = SetWindowLong(StyleButton.hwnd, GWL_STYLE, curstyle)
 
    StyleButton.Refresh
  
End Sub

Form Code


Private Sub Command1_Click()
  Call SetGraphicStyle(Option1, True)
End Sub

Private Sub Command2_Click()
 Call SetGraphicStyle(Option1, False)
End Sub
  

Go Back