Link to home
Start Free TrialLog in
Avatar of Dodsworth
Dodsworth

asked on

Paint Event Problem

I have a little class called Task that inherits from a picture box.  My form has Tasks as List(of Task) and a variable to store the currently selected Task.  When a Task is clicked it becomes the SelectedTask and the Paint event draws a red border around the control.  When another Task is clicked the previous Task should loose the red border and the new Task should become the current Task.

Problem is that the refresh on the previously selected Task doesn't fire.

Any ideas ?

Public Class Task

    Inherits Windows.Forms.PictureBox

    Private mTask As PictureBox

    Private isSelected As Boolean = False
   
    Public Sub New()
        AddHandler Me.Paint, AddressOf myTask_Paint
        AddHandler Me.MouseDown, AddressOf myTask_MouseDown
        Form1.Tasks.Add(Me)
    End Sub

    Private Sub myTask_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        If isSelected Then
            e.Graphics.DrawRectangle(New Pen(Color.Red, 1), New Rectangle(0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1))
        End If
    End Sub

    Private Sub myTask_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Stop
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If Not Form1.currentTask Is Nothing Then ' redraw the previously selected PicBox with no highlight
                isSelected = False
                Dim pb As PictureBox = DirectCast(Form1.currentTask, PictureBox) 'Hmm doesn't trigger the Paint event?
                pb.Refresh()
            End If
            isSelected = True ' redraw the newPicBox with highlight
            Form1.currentTask = Me
            Me.Refresh()
        End If
    End Sub
    
End Class

Open in new window


Public Class Form1

    Public Tasks As New List(Of Task)
    Public currentTask As Task

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim T1 As New Task With {
            .Name = "T1",
            .Size = New Point(20, 20),
            .Location = New Point(200, 200),
            .BackColor = Color.Blue}
        Me.Controls.Add(T1)

        Dim T2 As New Task With {
                   .Name = "T2",
                   .Size = New Point(20, 20),
                   .Location = New Point(300, 300),
                   .BackColor = Color.Blue}
        Me.Controls.Add(T2)

        Dim T3 As New Task With {
           .Name = "T3",
           .Size = New Point(20, 20),
           .Location = New Point(400, 400),
           .BackColor = Color.Blue}
        Me.Controls.Add(T3)
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HooKooDooKu
HooKooDooKu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dodsworth
Dodsworth

ASKER

OMG talk about glaringly obvious !

Thanks :)