Karolis
Karolis

Reputation: 89

Removing gaps in System.Array

I going to make application which displays pictures. I want to add "Delete" function, but i met problem. The problem is gaps between pictures. I want to remove them.

For example:

i have Array like this:
{1 = image_1, 2 = image_2, 3 = image_3}

when i remove image_2 i have:
{1 = image_1, 2 = nil, 3 = image_3}

I want:
{1 = image_1, 2 = Image_3}

Code:

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

Upvotes: 3

Views: 104

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564821

I would recommend storing your images in a different type of collection instead of an array, such as a List(Of T), or even a LinkedList(Of T).

These will allow you to remove an image from the collection directly, via List.Remove, etc.

Upvotes: 5

Related Questions