I am trying to create a rectangle on a picturebox with randome size and location. Every time the user click on a button, the rectangle will be removed and the new one will be displayed.
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
RandomBox()
End Sub
Public Sub RandomBox()
Dim r As New Rectangle(CInt(Rnd() * picGene.Width), CInt(Rnd() * picGene.Height), 20 + Rnd() * 50, 10 + Rnd() * 50)
Dim g As System.Drawing.Graphics
g = picGene.CreateGraphics()
g.FillRectangle(Brushes.Blue, r)
End Sub
It always keeps the rectangles drawn previously so I tried to place the picGene.Invalide() before or after the code block. It removes the old rectangle but it does not draw the new rectangle.
Public Sub RandomBox()
picGene.Invalidate()
Dim r As New Rectangle(CInt(Rnd() * picGene.Width), CInt(Rnd() * picGene.Height), 20 + Rnd() * 50, 10 + Rnd() * 50)
Dim g As System.Drawing.Graphics
g = picGene.CreateGraphics()
g.FillRectangle(Brushes.Blue, r)
'picGene.Invalidate()
End Sub
Any idea?
Thanks