Results 1 to 11 of 11

Thread: [RESOLVED] list.remove method

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Resolved [RESOLVED] list.remove method

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list.remove method

    In the Click event handler of the button you simply need to remove the last Line from the collection and repaint the control.
    VB Code:
    1. Dim removedLine As Line = Me.lines(Me.lines.Count - 1)
    2.  
    3. Me.lines.Remove(removedLine)
    4. Me.pctPaintBox.Invalidate(New Rectangle(Math.Min(removedLine.Start.X, removedLine.End.X), _
    5.                                         Math.Min(removedLine.Start.Y, removedLine.End.Y), _
    6.                                         Math.Abs(removedLine.Start.X - removedLine.End.X), _
    7.                                         Math.Abs(removedLine.Start.Y - removedLine.End.Y)))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: list.remove method

    Hello,

    thanks for the reply.
    But when I add 2 lines and I press the cancel button I get the following message:

    "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list.remove method

    Your original code is copied from my CodeBank submission so I'm fairly familiar with it. If you've added Line objects to that List then the code in post #2 will work exactly as you've asked. The ONLY reason that you might have an issue is if you continue to press your button after you've removed all the Lines. It's your responsibility to prevent that happening by disabling the button when you remove the last line and/or testing the Count property of the List before attempting to remove a Line.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list.remove method

    One more thing. You may need to expand the invalidated rectangle very slightly. In my testing it seemed to leave the odd pixel behind, so maybe just expand it by 1 in each direction to be safe.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: list.remove method

    Hello,

    I have tried to add 3 lines (works perfect).
    Then I press 1 time and I get the errormessage


    "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"


    The rectangle isn't even reached in the procedure.
    Because I get the error message on the:

    VB Code:
    1. Dim removedLine As Line = Me.lines(Me.lines.Count - 1)

    Statement

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: list.remove method

    I did a little more research in the code and it seems that me.lines.count stays 0.
    So when I add 3 lines the value of me.lines.count should be 3.
    Right???

    But how do I solve this??

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: list.remove method

    I have thrown out the clear method in the btnnext sub.
    Now it count but the code still doesnt work.

    I hope you guys can help me.
    I got the feelin' this problem is about to be solved.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: list.remove method

    Right now I got the code so far that when I press the cancel button I dont get the error message...
    BUT...
    I I have 3 lines and I press the cancel button I still see 3 lines.
    Do I press the button again I get the error message.

    I tried a refresh but that didn't work

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list.remove method

    OK, the whole idea of the List is so that it stores the Lines temporarily until you call Save, at which point the lines are drawn permanently on an Image object. The comments in the Save method specifically state that the lines are made permanent and removed from the List. Your code calls Save every time it creates a new Line, so it is immediately made permanent and removed from the List. If you want the ability to undo then don't call Save. That was the whole point of including that Save method in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: list.remove method

    jmcilhinney you are SUPERMAN!!!

    It works now...

    Everybody THANK you very very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width