Click here to Skip to main content
14,733,542 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to draw a line from one point to another on a Form and code it in Form_Load() event (not use Paint event):

private void Form1_Load(object sender, EventArgs e)
        {
            Pen pen = new Pen(Brushes.Blue,1);
            Graphics m = this.CreateGraphics();
            m.DrawLine(pen, 0, 0, 100, 100);
        }


But when i run, i dont get any result! Why? Hope you help me solve this problem. Thanks.
Posted
Updated 1-Nov-12 19:56pm
v2
Comments
   
It's natural to guess what Form1_Load is supposed to mean, but this is only a guess. It's wrong idea to draw like that, and even CreateGraphics is wrong. This is not how graphics works.
--SA

1 solution

Please see my comment to the question. This is not how Forms and System.Drawing work, not at all. Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA
   
Comments
Andrewpeter 2-Nov-12 9:29am
   
Thanks SA, i will try to do it. My vote is 5.
   
Great. Then please accept the answer formally (green button) -- thanks.
Your follow-up questions are welcome, of course.
--SA
Andrewpeter 3-Nov-12 5:17am
   
Yes!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900