By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,533 Members | 1,497 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,533 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"John" <Jo**@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
See the GDI+ FAQ #1 most asked question for the answer.

You'll find other articles on the use and misuse of CreateGraphics there
too.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"John" <Jo**@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.