Results 1 to 5 of 5

Thread: DrawLine

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    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?
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    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);

  3. #3

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Re: DrawLine

    Thanks That did the job
    How would I do it in a picturebox?
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    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);

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Re: DrawLine

    thanks
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width