Hello,

I use the following code to add multiple lines on a picturebox.
Each time I press the button next a line gets added to the list of lines.
Everything works fine (Thanks to the help of you guys).

Here comes my problem: When i press the cancel button I want to delete a line from the list.
So suppose I have 3 lines on my picturebox, the first time I press the cancel button the LAST line I added should be removed from the picturebox.
The second time I press the cancel button, the second line should be deleted and so on...

Also I would appreceate it if you could check my code if it is well programmed.

Thanks in advance.

VB Code:
  1. Public Class frmCutSelectedSheet
  2.  
  3.     Dim GraphicsFun As System.Drawing.Graphics
  4.     Dim PenColor As New System.Drawing.Pen(Color.Red)
  5.     Private lines As New List(Of Line)
  6.     Dim start As Point
  7.     Dim [end] As Point
  8.     Dim counter As Integer = 0
  9.     Dim Coordinates As String
  10.     Dim NumberOfCuts As Integer
  11.  
  12.  
  13.     Private Sub frmCutSelectedSheet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  14.         'Place a blank image in the PictureBox control.
  15.         Me.pctPaintBox.Image = New Bitmap(Me.pctPaintBox.Width, Me.pctPaintBox.Height)
  16.         lblStartingX.Text = "<= " & Me.pctPaintBox.Width - 1
  17.         lblStartingY.Text = "<= " & Me.pctPaintBox.Height - 1
  18.         lblEndingX.Text = "<= " & Me.pctPaintBox.Width - 1
  19.         lblEndingY.Text = "<= " & Me.pctPaintBox.Height - 1
  20.         Me.txtStartingX.Enabled = False
  21.         Me.txtStartingY.Enabled = False
  22.         Me.txtEndingX.Enabled = False
  23.         Me.txtEndingY.Enabled = False
  24.     End Sub
  25.  
  26.     Private Sub pctPaintBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pctPaintBox.Paint
  27.         'Draw each line on the control.
  28.         For Each line As Line In Me.lines
  29.             e.Graphics.DrawLine(Pens.Black, line.Start, line.End)
  30.         Next line
  31.     End Sub
  32.  
  33.     Private Sub Save()
  34.         'Create a Graphics object from the Image in the PictureBox.
  35.         Dim g As Graphics = Graphics.FromImage(Me.pctPaintBox.Image)
  36.  
  37.         'Draw each line on the image to make them permanent.
  38.         For Each line As Line In Me.lines
  39.             g.DrawLine(Pens.Black, line.Start, line.End)
  40.         Next line
  41.  
  42.         'Clear the temporary lines that were just saved.
  43.         Me.Clear()
  44.     End Sub
  45.  
  46.     Private Sub Clear()
  47.         'Clear all unsaved lines.
  48.         Me.lines.Clear()
  49.  
  50.         'Force the control to repaint so the lines are removed.
  51.         Me.pctPaintBox.Refresh()
  52.     End Sub
  53.  
  54.     Private Sub btnNextCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextCut.Click
  55.         If counter = 0 Then
  56.             Me.txtStartingX.Enabled = True
  57.             Me.txtStartingY.Enabled = True
  58.             Me.txtEndingX.Enabled = True
  59.             Me.txtEndingY.Enabled = True
  60.             counter += 1
  61.         Else
  62.             Call Me.Clear()
  63.             With Me.pctPaintBox
  64.                 If txtStartingX.Text >= .Width Then
  65.                     MsgBox("X coordinate startpoint is invalid")
  66.                     Exit Sub
  67.                 ElseIf txtStartingY.Text >= .Height Then
  68.                     MsgBox("Y coordinate startpoint is invalid")
  69.                     Exit Sub
  70.                 ElseIf txtEndingX.Text >= .Width Then
  71.                     MsgBox("X coordinate endingpoint is invalid")
  72.                     Exit Sub
  73.                 ElseIf txtEndingY.Text >= .Height Then
  74.                     MsgBox("Y coordinate endingpoint is invalid")
  75.                     Exit Sub
  76.                 Else
  77.  
  78.                     [end] = New Point(txtEndingX.Text, txtEndingY.Text)
  79.                     start = New Point(txtStartingX.Text, txtStartingY.Text)
  80.  
  81.                     'Add the new line to the list.
  82.                     Me.lines.Add(New Line(Me.start, [end]))
  83.  
  84.                     'Force the control to repaint so the new line is drawn.
  85.                     Me.pctPaintBox.Invalidate(New Rectangle(Math.Min(Me.start.X, [end].X), _
  86.                                                            Math.Min(Me.start.Y, [end].Y), _
  87.                                                           Math.Abs(Me.start.X - [end].X), _
  88.                                                          Math.Abs(Me.start.Y - [end].Y)))
  89.                     'Me.Refresh()
  90.                     Call Me.Save()
  91.  
  92.                     If Coordinates = "" Then
  93.                         Coordinates = "(" & txtStartingX.Text & "," & txtStartingY.Text & "," & txtEndingX.Text & "," & txtEndingY.Text & ")"
  94.                     Else
  95.                         Coordinates = Coordinates & "#" & "(" & txtStartingX.Text & "," & txtStartingY.Text & "," & txtEndingX.Text & "," & txtEndingY.Text & ")"
  96.                     End If
  97.  
  98.                     txtStartingX.Text = ""
  99.                     txtStartingY.Text = ""
  100.                     txtEndingX.Text = ""
  101.                     txtEndingY.Text = ""
  102.                     lblCuts.Text = "Cuts : " & Me.lines.Count
  103.                     counter += 1
  104.                 End If
  105.             End With
  106.         End If
  107.  
  108.     End Sub
  109.  
  110.     Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
  111.  
  112.     End Sub
  113. End Class
  114.  
  115. Public Class Line
  116.     'The line's start point.
  117.     Private _start As Point
  118.  
  119.     'The line's end point.
  120.     Private _end As Point
  121.  
  122.     'The line's start point.
  123.     Public Property Start() As Point
  124.         Get
  125.             Return Me._start
  126.         End Get
  127.         Set(ByVal value As Point)
  128.             Me._start = value
  129.         End Set
  130.     End Property
  131.  
  132.     'The line's end point.
  133.     Public Property [End]() As Point
  134.         Get
  135.             Return Me._end
  136.         End Get
  137.         Set(ByVal value As Point)
  138.             Me._end = value
  139.         End Set
  140.     End Property
  141.  
  142.     Public Sub New()
  143.         Me.New(Point.Empty, Point.Empty)
  144.     End Sub
  145.  
  146.     Public Sub New(ByVal start As Point, ByVal [end] As Point)
  147.         Me._start = start
  148.         Me._end = [end]
  149.     End Sub
  150. End Class