DrawLine() does not work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nilam2477
    New Member
    • Feb 2008
    • 38

    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.Draw ing.Color.Red, 5);
    System.Drawing. Graphics formGraphics = this.CreateGrap hics();
    formGraphics.Dr awLine(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?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.

    Comment

    Working...