Thank you for for your interest in my project. I've got as far as I can with the code. I've implemented the list and can track whether the rectangle has been clicked or not, using Contains. I've included a message box and some labels to temporarily keep track of what's happening. A rectangle is created on the form loading but I rather want it created on a button. However, Button1 creates a second rectangle on top of the first and disappears when clicked and I can't see how to update the picturebox_paint with different values to create additional rectangles. So far, I can also move the single rectangle around the picturebox. Any further advice would be appreciated.
Public Class Form1
Private rectangle As Rectangle = New Rectangle(70, 70, 100, 150)
Dim rectangles As List(Of Rectangle) = New List(Of Rectangle)()
Private isMouseDown As Boolean = False
Public Sub New()
InitializeComponent()
End Sub
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.FillRectangle(New SolidBrush(Color.Gray), rectangle)
End Sub
Private Sub pictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
isMouseDown = True
End Sub
Private Sub pictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If isMouseDown = True Then
Label1.Text = e.Location.ToString
If Contains(e.Location) = True Then
Label2.Text = True.ToString
Else
Label2.Text = False.ToString
End If
rectangle.Location = e.Location
If rectangle.Right > PictureBox1.Width Then
rectangle.X = PictureBox1.Width - rectangle.Width
End If
If rectangle.Top < 0 Then
rectangle.Y = 0
End If
If rectangle.Left < 0 Then
rectangle.X = 0
End If
If rectangle.Bottom > PictureBox1.Height Then
rectangle.Y = PictureBox1.Height - rectangle.Height
End If
Refresh()
End If
End Sub
Private Sub pictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
isMouseDown = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.CreateGraphics.FillRectangle(New SolidBrush(Color.Brown), rectangle)
rectangles.Add(rectangle)
MsgBox(rectangles.Count.ToString)
Dim sResult As String = ""
For i = 0 To rectangles.Count - 1
sResult = rectangles.Item(i).ToString
MsgBox("Rectangle " & i.ToString & " " & sResult)
Next
End Sub
Public Overloads Function Contains(ByVal pt As System.Drawing.Point) As Boolean
If rectangle.Contains(pt) Then
Return True
Else
Return False
End If
Return False
End Function
End Class