954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Code for drawing multiple lines on form?

Hello...

I want to draw three straight lines on a form.

Can you tell me if the following code is correct:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim Line1 As System.Drawing.Graphics
        Dim PenLine As New System.Drawing.Pen(System.Drawing.Color.Black)
        Line1 = Me.CreateGraphics
        Line1.DrawLine(PenLine, 0, 129, 529, 129)
        Line1.DrawLine(PenLine, 0, 180, 529, 180)
        Line1.DrawLine(PenLine, 0, 190, 529, 190)
    End Sub

Can I use only one variable (Line1) to draw three straight lines?
Is this the right way to do it?

ak24
Newbie Poster
17 posts since Dec 2010
Reputation Points: 23
Solved Threads: 0
 


It's almost right.

Don't create a graphics object here. Its a waste. Use the graphics object
in the paint event args.

Other than that your good to go.

e.graphics.DrawLine(PenLine, 0, 129, 529, 129)       
 e.graphics.DrawLine(PenLine, 0, 180, 529, 180)       
 e.graphics.DrawLine(PenLine, 0, 190, 529, 190

'clean up your resources.
 PenLine.dispose
Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

Thank you!

ak24
Newbie Poster
17 posts since Dec 2010
Reputation Points: 23
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: