Setting Style Property of OptionButton
and CheckBox at Runtime
"Can't assign to readonly property'"
Don't you just hate it when VB says
this? You want to change the style property of a option button
or check box, but it is read only at runtime. What can you do?
You could load up a second checkbox . . . or you could use the
API. In this neat little tip, you can change the style
property at run time:
Declarations
Copy this code into the declarations
section of a module:
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&
Procedure
Public Sub SetGraphicStyle(StyleButton As Control, _
Flag As Boolean)
Dim curstyle As Long
Dim newstyle As Long
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
|