I want to put an outline around each control on a panel, as a test case I used some checkboxes. It draws the outlines fine, but if I mouseover the checkboxes, they dont repaint. I cant see anything wrong with this (or I might have code blindness). Can anyone see what's wrong?
VB Code:
Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
Dim MyPanel As New Panel
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As IntPtr = New IntPtr(GetWindowDC(MyPanel.Handle.ToInt32))
Dim g As Graphics = Graphics.FromHdc(d)
PaintCon(MyPanel, g)
g.Dispose()
End Sub
Private Sub PaintCon(ByVal Con As Control, ByVal g As Graphics)
For Each ctl As Control In Con.Controls
If ctl.Controls.Count > 0 Then
PaintCon(ctl, g)
Else
If ctl.GetType.ToString.ToLower = "system.windows.forms.checkbox" Then
Dim ConRect As New Rectangle(MyPanel.PointToClient(ctl.Parent.PointToScreen(ctl.Location)), ctl.Size)
g.DrawRectangle(Pens.Black, ConRect)
RemoveHandler Con.Paint, AddressOf ConPaint
AddHandler Con.Paint, AddressOf ConPaint
End If
End If
Next
End Sub
Private Sub ConPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Dim con As Control = DirectCast(sender, Control)
Dim ConRect As New Rectangle(MyPanel.PointToClient(con.Parent.PointToScreen(con.Location)), con.Size)
I'm assuming your "ConPaint" method is supposed to be the paint event that is raised when the control is repainted? If so you need to add the paint handler for the controls to the method.
i.e.
VB Code:
Private Sub conPaint(sender as object, e as eventargs) [COLOR=Red]Handles CheckBox1.Paint[/COLOR]
You guys do things in 5 minutes that it takes me a whole day to build (and which usually doesnt work)! Thanks so much for doing that.
Your code is miles better, but looking at my original code, its not obvious to me what I was doing wrong? Just for my own learning, I guess I cant point at a paint event with a handler, was that the problem?
I did manage to duplicate the problem, but it was not immediately obvious why it was not working, I thought I would spend the time re doing rather than looking for the problem!!
I have changed the code slightly, you should really override the OnPaint event, its better practice:
VB Code:
''' <summary>
''' A Panel control that draws a rectangle around checkboxes
''' </summary>
''' <remarks></remarks>
Public Class BullDogPanel
Inherits Panel
Private m_drawRectangleAroundCheckBox As Boolean
<System.ComponentModel.Category("Appearance")> _
<System.ComponentModel.DisplayName("Draws a rectangle around any checkboxs on 'Me'")> _
Public Property DrawRectangleAroundCheckBox() As Boolean
Get
Return Me.m_drawRectangleAroundCheckBox
End Get
Set(ByVal value As Boolean)
Me.m_drawRectangleAroundCheckBox = value
Me.Invalidate()
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If Me.m_drawRectangleAroundCheckBox Then
For Each control As Control In Me.Controls
If TypeOf control Is CheckBox Then
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(control.Location.X - 1, control.Location.Y - 1, _