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

LVL 1
DodsworthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HooKooDooKuCommented:
Your problem is that in the MouseDown event, you are updating the isSelected value of the 'Me' task and not the current task on the form.

Try this:
    Private Sub myTask_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If Not Form1.currentTask Is Nothing Then ' redraw the previously selected PicBox with no highlight
                Form1.currentTask.isSelected = False
                Form1.currentTask.Refresh()
            End If
            isSelected = True ' redraw the newPicBox with highlight
            Form1.currentTask = Me
            Me.Refresh()
        End If
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DodsworthAuthor Commented:
OMG talk about glaringly obvious !

Thanks :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.