CodeItBetter Programming Another VB Programming Blog

How to set Option Button and Check Box Style Property At run time

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'Controls - How to set Option Button and Check Box Style Property At run time

Option Explicit
 
'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.

'Add a Option Button and two Command Buttons to your form.

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, 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
 
'How can I call this function
Private Sub Command1_Click()
    Call SetGraphicStyle(Option1, True)
End Sub
 
Private Sub Command2_Click()
    Call SetGraphicStyle(Option1, False)
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.