Public Class Form1
''' <summary>
''' Indicates whether or not each PictureBox is locked.
''' </summary>
Private lockedStateByPictureBox As Dictionary(Of PictureBox, Boolean)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'All PictureBoxes are initially unlocked.
Me.lockedStateByPictureBox = New Dictionary(Of PictureBox, Boolean) From {{Me.PictureBox1, False},
{Me.PictureBox2, False},
{Me.PictureBox3, False}}
End Sub
Private Sub PictureBoxes_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox3.MouseEnter,
PictureBox2.MouseEnter,
PictureBox1.MouseEnter
Dim pb = DirectCast(sender, PictureBox)
If Not Me.lockedStateByPictureBox(pb) Then
'This PictureBox is not locked so indicate that the cursor is over it.
pb.BackColor = Color.Red
End If
End Sub
Private Sub PictureBoxes_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox3.MouseLeave,
PictureBox2.MouseLeave,
PictureBox1.MouseLeave
Dim pb = DirectCast(sender, PictureBox)
If Not Me.lockedStateByPictureBox(pb) Then
'This PictureBox is not locked so indicate that the cursor is not over it.
pb.BackColor = Color.Green
End If
End Sub
Private Sub PictureBoxes_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click,
PictureBox2.Click,
PictureBox1.Click
Dim pb = DirectCast(sender, PictureBox)
If Not Me.lockedStateByPictureBox(pb) Then
'Unlock any currently locked PictureBox(es).
Unlock()
'Lock the PictureBox that was just clicked.
Lock(pb)
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Unlock any currently locked PictureBox.
Unlock()
End Sub
''' <summary>
''' Locks a PictureBox.
''' </summary>
''' <param name="pb">
''' The PictureBox to lock.
''' </param>
Private Sub Lock(pb As PictureBox)
'Indicate that the PictureBox is locked.
pb.BackColor = Color.Blue
Me.lockedStateByPictureBox(pb) = True
'Start the unlock Timer.
Me.Timer1.Start()
End Sub
''' <summary>
''' Unlocks any locked PictureBox.
''' </summary>
Private Sub Unlock()
'Stop the unlock Timer.
Me.Timer1.Stop()
For Each pb In Me.lockedStateByPictureBox.Keys.ToArray()
If Me.lockedStateByPictureBox(pb) Then
'Unlock the locked PictureBox.
Me.lockedStateByPictureBox(pb) = False
If Me.RectangleToScreen(pb.Bounds).Contains(Cursor.Position) Then
'Indicate that the cursor is over the PictureBox.
pb.BackColor = Color.Red
Else
'Indicate that the cursor is not over the PictureBox.
pb.BackColor = Color.Green
End If
'Only one PictureBox can ever be locked so look no further.
Exit For
End If
Next
End Sub
End Class