-
DrawLine
I am trying to draw a line on a form with the following code, but I get a strange error....
Code:
private void button2_Click(object sender, System.EventArgs e)
{
Graphics g;
Pen aPen = new Pen(Brushes.Black, 2);
g.DrawLine(aPen, 100,100,300,300);
}
Error:
C:\Documents and Settings\carsten\My Documents\Visual Studio Projects\sharp_test_obel\Form1.cs(228): Use of unassigned local variable 'g'
What am I doing wrong?
-
Re: DrawLine
You are not creatning the graphics object
try
Code:
Graphics g = this.CreateGraphics();
Pen aPen = new Pen(Brushes.Black, 2);
g.DrawLine(aPen, 100,100,300,300);
-
Re: DrawLine
Thanks :) That did the job
How would I do it in a picturebox?
-
Re: DrawLine
use the same method just create the graphics from the picturebox instead, as far as i know it can be done with any control
Code:
Graphics g;
g = this.pictureBox1.CreateGraphics();
Pen aPen = new Pen(Brushes.Black, 2);
g.DrawLine(aPen, 100,100,300,300);
-
Re: DrawLine