消除Sys​​tem.Array中的空白

时间:2013-05-27 17:03:30

标签: .net arrays vb.net

我将制作显示图片的应用程序。我想添加“删除”功能,但我遇到了问题。问题是图片之间的差距。我想删除它们。

例如:

我有这样的数组:
    {1 = image_1, 2 = image_2, 3 = image_3}

当我删除image_2时,我有:
    {1 = image_1, 2 = nil, 3 = image_3}

我想:
    {1 = image_1, 2 = Image_3}

代码:

Public Class Form1
Dim latestImageId As Integer = 0
Dim dispPictureId As Integer = 0
Dim images(128) As Image

Private Sub CapturePic(sender As Object, e As EventArgs) Handles TestToolStripMenuItem.Click
    If My.Computer.Clipboard.ContainsImage() Then
        latestImageId = (latestImageId + 1)
        images(latestImageId) = My.Computer.Clipboard.GetImage()
        If dispPictureId = 0 Then
            Label1.Text = dispPictureId
            dispPictureId = 1
            PictureBox1.Image = images(dispPictureId)
        End If
    End If
    Label1.Text = dispPictureId
End Sub

Private Sub Nex_Click(sender As Object, e As EventArgs) Handles Nex.Click
    If Not images(dispPictureId + 1) Is Nothing Then
        PictureBox1.Image = images(dispPictureId + 1)
    Else

        Exit Sub
    End If
    dispPictureId = (dispPictureId + 1) Mod images.Length
    Label1.Text = dispPictureId
End Sub

Private Sub Pre_Click(sender As Object, e As EventArgs) Handles Pre.Click
    If Not images(dispPictureId - 1) Is Nothing Then
        PictureBox1.Image = images(dispPictureId - 1)
    Else
        Exit Sub
    End If
    dispPictureId = (dispPictureId - 1) Mod images.Length
    Label1.Text = dispPictureId
End Sub

Private Sub Del_Click(sender As Object, e As EventArgs) Handles Del.Click
    images(dispPictureId) = Nothing
    If images(dispPictureId) Is Nothing Then
        dispPictureId = dispPictureId - 1
    End If
    dispPictureId = 1
    Label1.Text = dispPictureId
End Sub
End Class

1 个答案:

答案 0 :(得分:4)

我建议您将图像存储在不同类型的集合中而不是数组中,例如List(Of T),甚至是LinkedList(Of T)

这些将允许您通过List.Remove等直接从集合中删除图像。