0

我有一系列线,我想在某个时候删除其中的一些。这是代码示例:

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

执行程序后,将创建行。但是当我给我的文本框赋予一个小于变量'it'的值时,它只会删除最后一行而不是其余的。我在调试时也看到数组的大小减小了。那么这意味着超出大小的内容仍然保留吗?这是为什么?。任何帮助表示赞赏。谢谢。

编辑:我试图创建这样的列表:

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

但是表格中的线条仍然可见。我对其进行了调试,发现列表大小减小了。当我有一个数组时同样的问题。怎么回事?...

4

2 回答 2

1

我建议更改iter.Textcint(iter.Text),因为它有可能将两个值作为文本进行比较(比较方式不同)。

我还建议更改Dim lines(20) As PowerPacks.LineShapeDim lines As new generic.list(of PowerPacks.LineShape)

这样您就不必担心ReDim Preserve(在循环中执行时可能会很慢),并且如果您愿意,您可以轻松地将项目插入任何索引

于 2014-05-07T13:36:06.530 回答
0

你应该Option Strict On在你的项目中使用,以避免类型之间的隐式转换,这可能会给你带来错误,或者更糟糕的是,出现意外行为。

另一方面,TextBox除非有需要,否则您不应该存储号码。NumericUpDown例如,使用。查看MSDN 文档

现在,对于数组,我建议使用List,它实现了处理元素所需的所有方法,并且有一个.ToArray()方法可以在需要时为您提供数组。

尝试这样的事情:

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

我打算给你写一个例子,但我意识到我不知道你到底想做什么。你能解释一下吗?

于 2014-05-07T14:10:15.477 回答