Drawing rectangle when the form loads

SSb

Member
Joined
Sep 28, 2005
Messages
14
Programming Experience
Beginner
Hello All,

In my windows form I have a picture-box PB1, and a button Bdraw. I want that
(1) when the form loads a rectangle is drawn on PB1
(2) later if the button Bdraw is pressed another rectangle is drawn.
The latter part (2) is ok, it works.

However I cannot get (1) to work.

The way I'm doing it is writting .....

Dim paper As Graphics
paper = PB1.CreateGraphics()
Dim mypen As Pen = New Pen(Color.Brown)
paper.DrawRectangle(mypen, 10, 10, 10, 10)

.... inside ->

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
<<here>>
End Sub

Why does this not work? And how can I get it to work.

Thanks,
SSB
 
When doing drawing using GDI+ you need to do it in the Paint event handler so that it gets redrawn each time the form gets repainted. If you do it in the Load event handler the form hasn't been painted yet, so as soon as it does your drawing ceases to exist.
 
Back
Top