I'm using the code

Sub PicCanvasMouseDown(sender As Object, e As MouseEventArgs)
	XPrev = e.X
	YPrev = e.Y
End Sub
	
Sub PicCanvasMouseUp(sender As Object, e As MouseEventArgs)
	XCur = e.X
	YCur = e.Y
End Sub
	
Sub PicCanvasPaint(sender As Object, e As PaintEventArgs)
	Dim p As New Pen(Color.Black, 10)
	Dim g As Graphics = e.Graphics
	g.DrawLine(p, XPrev, YPrev, XCur, YCur)
	PicCanvas.Invalidate()
End Sub

To draw a line on a PictureBox. It works fine, until you go to draw another line, when the first line dissapears, i presume this is because i'm using the same integer variables for each one (XPrev, YPrev, XCur, YCur.) How would i be able to use the same integer variables, but be able to draw more than one line ?

Hi,
> Save the All lines Points (Make a class Line Has X and Y member and Save it in an ArrayList)
> Draw All the Lines (ArrayList) in Paint Event

That's not really what i meant. I'm capturing the points on mouse up and down, and that works, but only for one line. i want to be able to keep that line on the picturebox, and draw another, using the same variables for capturing.

Yes, I am also meant the same thing.

To draw all the lines you need to store it. so you should have an ArrayList or some other dynamic data structure.

ArrayList can have any object. so you need to group your Line into a class like

Public Class Line
   Public StartPoint As Point
   Public EndPoint As Point
End Class

Then create a new Line object whenever you draw line.

Sub PicCanvasMouseUp(sender As Object, e As MouseEventArgs)
	XCur = e.X
	YCur = e.Y
        Dim CurLine As New Line
        CurLine.StartPoint = New Point( XPrev,YPrev)
        CurLine.EndPoint = e.Location
        'Now Add to ArrayList for further use
        ArrayList.Add ( CurLine )
End Sub

To repaint, Iterate through the ArrayList and Draw all the Lines in the ArrayList.

Sub PicCanvasPaint(sender As Object, e As PaintEventArgs)
	Dim p As New Pen(Color.Black, 10)
	Dim g As Graphics = e.Graphics

        'Here Iterate all the Lines in the ArrayList and Draw It

	PicCanvas.Invalidate()
End Sub

This is what i meant.

This article has been dead for over six months. Start a new discussion instead.