Owner Draw Radio Button

R

Rlrcstr

Trying to place a few radio buttons where I draw the image on the button. I declare an array of three buttons and initialize them as below. The paint event is getting triggered for the buttons, but my drawing never shows up. What am I doing wrong? Thanks.


For x As Integer = 0 To 2
drawStyle(x) = New RadioButton
With drawStyle(x)
.Width = pnlPalette.Width - 4
.Height = 24
.Appearance = Appearance.Button
.FlatStyle = FlatStyle.Popup
.Left = 2
.Tag = x
End With
pnlPalette.Controls.Add(drawStyle(x))
AddHandler drawStyle(x).Paint, AddressOf drawStyle_Paint
Next

---------------------------

Private Sub drawStyle_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim r As RadioButton = sender
Dim g As Graphics = r.CreateGraphics()

With r
If (.Tag = "One") Then g.FillRectangle(New SolidBrush(_fillColor), 2, 2, .Width - 4, .Height - 4)
If (.Tag = "Two") Then g.DrawRectangle(New Pen(_lineColor, _lineWidth), 2, 2, .Width - 4, .Height - 4)
End With
End Sub
 
H

Herfried K. Wagner [MVP]

For x As Integer = 0 To 2
drawStyle(x) = New RadioButton
With drawStyle(x)
.Width = pnlPalette.Width - 4
.Height = 24
.Appearance = Appearance.Button
.FlatStyle = FlatStyle.Popup
.Left = 2
.Tag = x
End With
pnlPalette.Controls.Add(drawStyle(x))
AddHandler drawStyle(x).Paint, AddressOf drawStyle_Paint
Next<<<

Instead of adding a handler to the buttons' 'Paint' events, create a class
that inherits from 'RadioButton' and overrides its 'OnPaint' method. Place
your drawing code in the 'OnPaint' method and use the derived class instead
of the standard radio button control.
 
J

Jerry Camel

Perfect. Thanks.


Herfried K. Wagner said:
For x As Integer = 0 To 2
drawStyle(x) = New RadioButton
With drawStyle(x)
.Width = pnlPalette.Width - 4
.Height = 24
.Appearance = Appearance.Button
.FlatStyle = FlatStyle.Popup
.Left = 2
.Tag = x
End With
pnlPalette.Controls.Add(drawStyle(x))
AddHandler drawStyle(x).Paint, AddressOf drawStyle_Paint
Next<<<

Instead of adding a handler to the buttons' 'Paint' events, create a class
that inherits from 'RadioButton' and overrides its 'OnPaint' method. Place
your drawing code in the 'OnPaint' method and use the derived class instead
of the standard radio button control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top