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?

Recommended Answers

All 2 Replies

Member Avatar for Unhnd_Exception

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.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.