sinobi
sinobi

Reputation: 29

How to draw graph in c#

I'm new ,and I don't understand why this method when copied to windows forms doesn't do anything when running the program. I was copy it from MSDN page.

public void DrawLinesPoint(PaintEventArgs e)
        {

            // Create pen.
            Pen pen = new Pen(Color.Black, 3);

            // Create array of points that define lines to draw.
            Point[] points =
                     {
                 new Point(10,  10),
                 new Point(10, 100),
                 new Point(200,  50),
                 new Point(250, 300)
             };

            //Draw lines to screen.
            e.Graphics.DrawLines(pen, points);
        }

Upvotes: 0

Views: 7299

Answers (1)

Clint
Clint

Reputation: 6499

Typically, when you draw on a form, you handle the form’s Paint event and perform the drawing using the Graphics property of the PaintEventArgs

In your code you need to add the DrawLinesPoint to the paint event before being able to use it

In your Constructor() add

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

And in your Paint PaintEventHandler

private void DrawLinesPoint(object sender, PaintEventArgs e)
{
    Pen pen = new Pen(Color.Black, 3);

    // Create array of points that define lines to draw.
    Point[] points =
                {
            new Point(10,  10),
            new Point(10, 100),
            new Point(200,  50),
            new Point(250, 300)
        };

    //Draw lines to screen.
    e.Graphics.DrawLines(pen, points);
}

Upvotes: 2

Related Questions