i am trying to draw a line between two points
like this:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics MyGraphics = e.Graphics;
            Pen NewPen = new Pen(Color.Red,5);
            MyGraphics.DrawLine(NewPen,StartDraw,EndDraw);
        }

it gives me an error:
Object reference not set to an instance of an object.
what have i done wrong ?

What about StartDraw and EndDraw? Is they initialized properly?

i dont have those functions.
what i'm trying to do is paint line on event.
i want the line to be painted only after i push a button.
so on my button click handler i just called
Form1_Paint.
i guess this is wrong.

ok here is the answare:

void paintArc(Point Start,Point End)
        {
            Graphics graph = this.CreateGraphics();
            Pen penCurrent = new Pen(Color.Red,3);
            graph.DrawLine(penCurrent,Start,End);
        }

origional code not working

Graphics graph = this.CreateGraphics();
Pen penCurrent = new Pen(Color.Red,3);
graph.DrawLine(penCurrent,Start,End);

working code

Graphics graph = this.CreateGraphics(); 
Pen penCurrent = new Pen(Color.Red, 3); 
graph.DrawLine(penCurrent, 0,0, 100,100); 

the computer does not know the definitions start and end
it is possible to define them as points first
Point new as follows

Graphics graph = this.CreateGraphics(); 
Pen penCurrent = new Pen(Color.Red, 3);
Point Start = new Point(0, 0);
Point End = new Point(100, 100);
graph.DrawLine(penCurrent,Start, End);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.