I have a PictureBox, with an floor plan image.
On that image I would like to draw a polygon, which I can with the following code:
Private points As New List(Of Point)
Private Sub FlorPlan_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FlorPlan.MouseDown
points.Add(e.Location)
FlorPlan.Invalidate(New Rectangle(e.X, e.Y, 5, 5))
End Sub
Private Sub FlorPlan_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles FlorPlan.Paint
For Each p As Point In points
e.Graphics.FillRectangle(Brushes.Red, p.X, p.Y, 5, 5)
Next
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Red, 2)
Dim myGraphics As Graphics = PictureBox.CreateGraphics
myGraphics.DrawPolygon(myPen, points.ToArray)
'myGraphics.FillPolygon(Brushes.Red, points.ToArray)
myGraphics.Dispose()
End Sub
What I would like to do is to get hold of that object, so that I later can change the background color, etc.
How should I proceed?
Their are two ways this would typically be done:
If you choose to do the latter, I would recommend creating your own
UserControlwhich contains all the necessary data about each graphical object, such as the points in the polygon. To dynamically load them, you could do something like this:You will probably want to add some event handlers to some of the events on the control too as you add them.