473,325 Members | 2,442 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,325 software developers and data experts.

DrawLine() does not work

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 Form1_Paint() method it works.
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 10, 10, 150, 200);

Why does the code not work in Form_Load(). Is is required that all graphics code needs to be executed in Form_Paint() method only?
May 13 '08 #1
1 1779
Plater
7,872 Expert 4TB
It does work.
Except that it will be "erased" the next time the form refreshes itself, which is very very often.
The paint method is called whenever it refreshes, so putting it in there ensures that it gets re-drawn every time.

EDIT:
If you just want an arbitrary line, like for dividing purposes, you could always add an instance of "Panel" to your form, set it's background color to the color you want for your line (Black?) and then resize it to be a line.
May 13 '08 #2

Sign in to post your reply or Sign up for a free account.

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...
4
by: Smoke | last post by:
It is possible to draw a line on a control using AA? basically, i need the normal .DrawLine funcion but that support antia aliasing. is this possible? can somebody tellme how? Thanks for the...
2
by: google_groups3 | last post by:
Hi, is it better practise to draw a line on my form using the Graphics.DrawLine command or should I use a label control instead? I am refering to overall speed of the form and amount of memory...
2
by: John | last post by:
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...
0
by: Lila Godel | last post by:
I am migrating my VB6 GoldMine add-ons to VB.NET to allow for enhancements. I have no problem getting text to print, but I cannot get the 4 PrintPage events that are needed to print a line to work....
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...
3
by: Ben Bush | last post by:
I had the following code and when I clicked the left mouse button one time. I got green line and the second click got a purple line and the green disappeared. I was confused by two questions:...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.