title: Visual Basic GDI+ Drawing Lines
body: <p>I am working on a simple VB interface that should allow the user to draw a line from the point the mouse is clicked down to whereever the mouse position is, then leave the line where the mouse button is released. I am new to VB GDI+ and could use some pointers or tips. I need it so that I can draw multiple lines on the same control panel and everytime I start a new line somehow make it that the Paint paint function does not have to loop through each line I already drew. Here is my Code:
      Public Class Form1</p>
  
  <pre><code>Dim down = False
  Dim ptA As Point
  Dim ptB As Point
  Dim ptC As Point
  Dim ptStart As New List(Of Point)    ' Starting point of each line
  Dim ptEnd As New List(Of Point)      ' Ending Point of Each line
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Me.DoubleBuffered = True
  End Sub
  
  Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseDown
      down = True
      ptA = New Point(e.X, e.Y)
  End Sub
  
  Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseMove
      If down = True Then
          ptB = New Point(e.X, e.Y)
          Win.Invalidate()    
      End If
  End Sub
  
  Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseUp
      down = False
      ptC = New Point(e.X, e.Y)
      ptStart.Add(ptA)
      ptEnd.Add(ptC)
  End Sub
  
  Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Win.Paint
      e.Graphics.Clear(Color.Black)
      e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
      For i = 0 To ptStart.Count - 1
          Win.CreateGraphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
      Next
  End Sub
  
  
  End Class
  </code></pre>
  
  <p>In order to keep my existing lines, I stored the points in a list and each time I move the mouse while drawing, I call the Paint function and each time it is called, it loops through each point and draws a line between them. Any suggestions would be greatly appreciated. </p>
  
  <p>Thank you,</p>
  
  <p>JD</p>
  
  <p>*<em>*</em> Edit</p>
  
  <pre><code>Public Class Form1
  
  Dim down = False
  Dim ptA As Point
  Dim ptB As Point
  Dim ptC As Point
  Dim ptStart As New List(Of Point)
  Dim ptEnd As New List(Of Point)
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Me.DoubleBuffered = True
  End Sub
  
  Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseDown
      down = True
      ptA = New Point(e.X, e.Y)
  End Sub
  
  Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseMove
      If down = True Then
          ptB = New Point(e.X, e.Y)
          Win.Invalidate()
      End If
  End Sub
  
  Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseUp
      down = False
      ptC = New Point(e.X, e.Y)
      ptStart.Add(ptA)
      ptEnd.Add(ptC)
  
  End Sub
  
  Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Win.Paint
      e.Graphics.Clear(Color.Black)
      e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
      For i = 0 To ptStart.Count - 1
          e.Graphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
      Next
  End Sub
  
  
  End Class
  </code></pre>