Results 1 to 6 of 6

Thread: Lines/Shape Control

  1. #1

    Thread Starter
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442

    Lines/Shape Control

    How to draw an line on a form in VB.NET?
    In vb6 we had a simple Control for that... Anyone can tell me how?
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  2. #2
    New Member
    Join Date
    Feb 2003
    Location
    Maputo, Moçambique
    Posts
    13
    First you create a pen object to draw the line with. Then you draw the line with the CreateGraphics.DrawLine method, sending the pen, starting and ending points as arguments.

    Example:

    VB Code:
    1. Dim blackPen As New Pen(Color.Black)
    2.  
    3. CreateGraphics.DrawLine(blackPen, 0, 0, 50, 50)


    /Sara

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Be sure to import System.Darwing

    then one simple way maybe this
    VB Code:
    1. Dim g As Graphics = Me.CreateGraphics
    2.         Dim p As Pen = New Pen(Color.Black)
    3.         g.DrawLine(p, 0, 0, 200, 200)
    4.         g.Dispose()
    5.         p.Dispose()

  4. #4

    Thread Starter
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442
    Thanks for the reaction, but none of the examples work... There aren't any lines drawn on my form

    /edit
    Aargh, it does work... only in the Form_Paint Sub, or am i wrong?
    Last edited by phrozeman; Feb 12th, 2003 at 09:59 AM.
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Maybe you are putting the code in form load event, I dont know y it does not draw in form load, I will tell you if i find the reason.

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Ok, I find out the reason,
    When you draw a line in form load event, at first the line is drwan then the form paints, so it earases the line. You should put it in onPaint event if you want to draw the line when form loads.

    VB Code:
    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2.     e.Graphics.DrawLine(Pens.Black,0,0,50,50)
    3.     End Sub

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