Deleting Lines from Array

416 Views Asked by At

I have an array of lines and I want at some point to erase some of them. Here's a sample of the code:

Dim canvas As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
Dim lines(20) As PowerPacks.LineShape
Dim it As Integer = 0

Private Sub GoldenSpi_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    canvas.Parent = Me
    lines.Initialize()
    iter.Text = 0
End Sub
Private Sub iter_TextChanged(sender As Object, e As EventArgs) Handles iter.TextChanged
   If (it > iter.Text And iter.Text <> 0) Then
        ReDim Preserve lines(iter.Text - 1)
   End If
   If (it <> iter.Text) Then
        it = iter.Text
   End If

   For i As Integer = 1 To iter.Text
      lines(i - 1) = New PowerPacks.LineShape(canvas)
      lines(i - 1).StartPoint = pA(i)
      lines(i - 1).EndPoint = pB(i)
      lines(i - 1).BringToFront()
   Next
End Sub

After I execute the program, the lines are created. But when I give a value to my textbox that is smaller than the variable 'it', it justs delete the last line and not the rest. Also I saw while debugging that the size of array is reduced. So that means that the contents beyond the size are still kept? Why is that?. Any help is appreciated. Thanks.

EDIT: I tried to create the List like this:

Dim lines As New Generic.List(Of PowerPacks.LineShape)

Private Sub iter_ValueChanged(blabla) Handles iter.ValueChanged
    If (it > iter.Value And iter.Value <> 0) Then
        lines.RemoveRange(iter.Value - 1, lines.Count - iter.Value)
   End If

   For i As Integer = 1 To iter.Value
      InitPoints()

      If i - 1 = lines.Count Then
        Dim line As New PowerPacks.LineShape
        With line
            .StartPoint = pA(i)
            .EndPoint = pB(i)
            .BringToFront()
            .Parent = canvas
        End With
        lines.Add(line)
      End If
   Next

End Sub

But still the lines are visible in the form. I debugged it and saw that the list size decreased. The same problem when I had an array. What is going?...

2

There are 2 best solutions below

0
On

You should use Option Strict On in your project, in order to avoid implicit conversion between types which can give you errors or, worse, unexpected behaviors.

On the other hand, you should not have a TextBox to store numbers unless there is a need. Use a NumericUpDown, for example. Take a look at the MSDN Documentation.

And now, for the array, I recommend using a List, which has all the methods implemented that you need to handle the elements, and has a .ToArray() method that will give you the array if needed.

Try something like this:

Dim it As Integer = 0
Dim lines As New List(Of PowerPacks.LineShape)()

Sub iter_TextChanged(sender As Object, e As EventArgs) Handles iter.TextChanged
    Dim iTxt As Integer

    Try 
        iTxt = Integer.Parse(iter.Text)

        If it > iTxt AndAlso iTxt <> 0 Then

        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

I was going to write to you an example, but I realized that I don't know exactly what you're trying to do. Could you explain?

0
On

I recommend changing iter.Text to cint(iter.Text), as there is a chance it's comparing both values as text (which is compared differently).

I'd also recommend changing Dim lines(20) As PowerPacks.LineShape to Dim lines As new generic.list(of PowerPacks.LineShape)

That way you don't have to worry about ReDim Preserve (which can be slow when you do it in a loop), and you can easily insert items into any index if you whish