DrawLine(Pens.Green, 0, 0, 30, 30) does not draw this time?

G

Guest

I created a number of pictureboxes in a panel, and want to draw lines in
those pictureboxes but I cannot. Please see the following code and make
corrections.
Thanks.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Panel1.AutoScroll = True
Panel1.Width = 200
Panel1.Height = 200
Dim max As Integer = 8
Dim p As PictureBox
Dim i As Integer
For i = 0 To max
p = New PictureBox
p.BorderStyle = BorderStyle.FixedSingle
p.BackColor = Color.FromArgb(CLng(-1))
p.Top = i * (Panel1.Height - 30) + i * 10 + 10
p.Left = 10
p.Height = Panel1.Height - 32
p.Width = Panel1.Width - 32
Dim gr As Graphics = p.CreateGraphics
gr.DrawLine(Pens.Green, 0, 0, 30, 30)
Panel1.Controls.Add(p)
Next

'Panel1.Controls.Add(p)
'Panel1.Controls.Item(i).Top = i * (Panel1.Height - 30) + i * 10 + 10
'Panel1.Controls.Item(i).Left = 10
'Panel1.Controls.Item(i).Height = Panel1.Height - 32
'Panel1.Controls.Item(i).Width = Panel1.Width - 32
'Dim gr As Graphics = Panel1.Controls.Item(i).CreateGraphics
'gr.DrawLine(Pens.Green, 0, 0, 30, 30)

End Sub
 
R

Robin Tucker

What you did was "correct", however nothing was rendered on the control
surface, as the control was not visible at the time. If you try adding your
panel to the controls set first, then rendering, you will probably see your
line. Also, remember that windows sends "paint" messages to a control
whenever a part of it has been trashed and needs repainting. Your line will
not be permanently etched onto the control, you will have to draw it
everytime the Paint message is received. This means overriding the paint
handlers on the control or the parent control.
 
B

Bob Powell [MVP]

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top