AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

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

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/