none
Drawing lines in windows forms

    Question

  • I am new to this and have a simple question, hopefully. How do you draw lines between controls or just draw lines in a windows form? I have several buttons and labels for a visual presentation of data. I want to have lines drawn between these. I appreciate any help. Kris.

    Monday, December 03, 2007 8:55 AM

Answers

  • Hi Kris, If I understood you correctly you are trying to use GDI+ to draw into your WinForm. Well, this subject is very wide and you can do many different things so it is impossible for me to explain you everything GDI+ can do but I can give you a simple start: Drawing Lines (Arbitrary position).

    This code Draws two lines, one horizontal and the other with an angle:

     

    Put this code on the Form Paint event:

    Code Block

    private void Form1_Paint(object sender, PaintEventArgs e)

    {

    Graphics g = e.Graphics;

    g.DrawLine(Pens.Black, new Point(10, 10), new Point(300, 10));

    g.DrawLine(Pens.Red, new Point(10, 50), new Point(300, 130));

    }

     

     

    Fist you create a Graphics object for the form, so from there you can start Drawing.

    The second line use one of the many GDI+ methods: DrawLine. It draws a black line based on two points as you can see.

    Same in the third line, but this time the Line is Red.

    There is not too much secret about drawing lines, so if you only need this you won't have any problem figuring your way out through the drawing.

     

    Hope this helps,

    Regards,

    Fábio

    Monday, December 03, 2007 10:40 AM
  • This should get you started in right direction.

     

    1st.) add an eventhandler to the paint event of the form like so...

    this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyForm_Painted);

     

    2nd.)Write the handler like so...

    private void MyForm_Painted(object sender, System.Windows.Forms.PaintEventArgs e)

    {

    Control previousCtrl = null;

    foreach(Control ctrl in this.Controls)

    {

    if(previousCtrl != null) {

    Point firstPoint = new Point(previousCtrl.Bounds.X,previousCtrl.Bounds.Y);

    Point secondPoint = new Point(ctrl.Bounds.X,ctrl.Bounds.Y);

    Pen blackPen = new Pen(Color.Black);

    e.Graphics.DrawLine(blackPen,firstPoint,secondPoint);

    }

    previousCtrl = ctrl;

    }

    }

     

     

    Monday, December 03, 2007 10:59 AM

All replies

  • Hi Kris, If I understood you correctly you are trying to use GDI+ to draw into your WinForm. Well, this subject is very wide and you can do many different things so it is impossible for me to explain you everything GDI+ can do but I can give you a simple start: Drawing Lines (Arbitrary position).

    This code Draws two lines, one horizontal and the other with an angle:

     

    Put this code on the Form Paint event:

    Code Block

    private void Form1_Paint(object sender, PaintEventArgs e)

    {

    Graphics g = e.Graphics;

    g.DrawLine(Pens.Black, new Point(10, 10), new Point(300, 10));

    g.DrawLine(Pens.Red, new Point(10, 50), new Point(300, 130));

    }

     

     

    Fist you create a Graphics object for the form, so from there you can start Drawing.

    The second line use one of the many GDI+ methods: DrawLine. It draws a black line based on two points as you can see.

    Same in the third line, but this time the Line is Red.

    There is not too much secret about drawing lines, so if you only need this you won't have any problem figuring your way out through the drawing.

     

    Hope this helps,

    Regards,

    Fábio

    Monday, December 03, 2007 10:40 AM
  • This should get you started in right direction.

     

    1st.) add an eventhandler to the paint event of the form like so...

    this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyForm_Painted);

     

    2nd.)Write the handler like so...

    private void MyForm_Painted(object sender, System.Windows.Forms.PaintEventArgs e)

    {

    Control previousCtrl = null;

    foreach(Control ctrl in this.Controls)

    {

    if(previousCtrl != null) {

    Point firstPoint = new Point(previousCtrl.Bounds.X,previousCtrl.Bounds.Y);

    Point secondPoint = new Point(ctrl.Bounds.X,ctrl.Bounds.Y);

    Pen blackPen = new Pen(Color.Black);

    e.Graphics.DrawLine(blackPen,firstPoint,secondPoint);

    }

    previousCtrl = ctrl;

    }

    }

     

     

    Monday, December 03, 2007 10:59 AM
  •  

    Thank you. It worked just fine.
    Friday, December 07, 2007 7:32 AM