Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Drawing inside winforms

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Taiwan
    Posts
    37

    Resolved [RESOLVED] [2.0] Drawing inside winforms

    i'm working on winforms right now.
    and i'm a little curious, whether i can draw a line, or maybe a shape in my forms by using tool like paintbrush or something.

    i've done some search, and all i can see from msdn is that : all are drawn programmatically.

    does .net doesn't support other tool ?

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: [2.0] Drawing inside winforms

    The System.Drawing namespace can be used for draing lines, arc's painting etc. The .net framework doesn't provide any components that allow you to drag on a line, but there may be some 3rd party components.

    I usually take care of any drawing requirements I have (mostly horozontal lines) by using a group box with no text.

    Regards,

    Darren

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Drawing inside winforms

    If you want to draw on a control you should do so in its Paint event handler. You set up control variables at the class level and then tell the control to repaint itself. In the Paint event handler you read those control variables and use them to draw on the control. The reason you use the Paint event handler is that every time the control is repainted any existing drawing is lost, so you need to redraw every time the control is painted. Here's some code that will draw a line in a random position on a form each time you click a button:
    Code:
    private Point start = Point.Empty;
    private Point end = Point.Empty;
    private Random rand = new Random();
    
    private void button1_Click(object sender, EventArgs e)
    {
        // Select random points for the line to start and end at.
        this.start = new Point(this.rand.Next(0, this.ClientSize.Width),
                               this.rand.Next(0, this.ClientSize.Height));
        this.end = new Point(this.rand.Next(0, this.ClientSize.Width),
                             this.rand.Next(0, this.ClientSize.Height));
    
        // Tell the form to repaint.
        this.Refresh();
    }
    
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Draw the line at the current coordinates.
        e.Graphics.DrawLine(Pens.Black, this.start, this.end);
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2006
    Location
    Taiwan
    Posts
    37

    Re: [2.0] Drawing inside winforms

    thx jm, thx duck..

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