473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
2 3011
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jon Cosby | last post by:
I'm using this to draw rectangles in a PictureBox image. Not all of the rectangles are complete, and after drawing several, some of them start disapearing. What's the reason for this? Pen...
1
by: Rich | last post by:
Hello, I am trying to draw a graph on a form. I can draw the box using drawline, and bars inside the box using drawrectangle, but when I try to draw a line inside the box the inner lines are...
2
by: dan heskett | last post by:
I am owner-drawing a listbox, in an attempt to create a nice list with some custom "fields" and text layout. Essentially it works, but I must be missing something big, conceptually, because I...
2
by: Stu | last post by:
Hi, Why does the code below only draw the last line of the loop? I was expecting this to draw 10 parallel lines. More to the piont....how do I get it to draw 10 parallel lines? Public...
2
by: intrepid_dw | last post by:
All I am writing a C# WinForms application which is giving me some problems. The application consists of a form containing an empty Tab control to which TabPages are added dynamically (at...
1
by: ssims | last post by:
I've got this code for a random GDI+ chart generator, and the demo works, but when I try to run it on my local machine I get a compiler error: Compiler Error Message: CS0117: 'Random' does not...
2
by: Tyreec | last post by:
Hi all, I am having an issue when trying to draw a simple line. I have created a Windows Application and created a Paint method atrached to the event Paint. Inside I create a Pen and draw a...
1
by: Nilam2477 | last post by:
I'm trying to draw a line on the form in C#. When i place the below draw line code in Form1_Load() method it does not draw the line when application starts. But if the same code is placed in...
9
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.