Listbox items out of range

Asked

Viewed 77 times

1

I have a list box on a timer with items and I need to pass all items one by one until the end and then go to the first index and stop the timer. But this gives me out of reach when it passes the last one. How can I resolve this exception? I googled but can’t find any solution. Thank you in advance.

This is my code

If (ListBox1.SelectedItem = Nothing) Then
        ListBox1.SelectedIndex = 0
        Timer1.Stop()
    Else
        ListBox1.SelectedIndex += 1
        gethash()
    End If

I have also Try like this

If (ListBox1.SelectedIndex >= ListBox1.Items.Count) Then
        ListBox1.SelectedIndex = 0
        Timer1.Stop()
    Else
        ListBox1.SelectedIndex += 1
        gethash()
    End If

And the result is the same

This is all the code

Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.Net
Public Class Form1
Dim SourceFileOpen As New System.Windows.Forms.OpenFileDialog
Dim md5hashindexer = 1
Dim percent = 0
Dim red
Dim i As Integer
Dim str As String = ""
Dim ix As Integer = 0

#Region "Atalhos para o principal hash_generator função"


Function md5_hash(ByVal file_name As String)

    Return hash_generator("md5", file_name)

End Function

Function sha_1(ByVal file_name As String)

    Return hash_generator("sha1", file_name)

End Function

Function sha_256(ByVal file_name As String)

    Return hash_generator("sha256", file_name)

End Function

#End Region
Function hash_generator(ByVal hash_type As String, ByVal file_name As String)


    Dim hash
    If hash_type.ToLower = "md5" Then

        hash = MD5.Create
    ElseIf hash_type.ToLower = "sha1" Then

        hash = SHA1.Create()
    ElseIf hash_type.ToLower = "sha256" Then

        hash = SHA256.Create()
    Else
        MsgBox("Type de hash inconnu : " & hash_type, MsgBoxStyle.Critical)
        Return False
    End If


    Dim hashValue() As Byte


    Dim fileStream As FileStream = File.OpenRead(file_name)

    fileStream.Position = 0

    hashValue = hash.ComputeHash(fileStream)

    Dim hash_hex = PrintByteArray(hashValue)


    fileStream.Close()


    Return hash_hex

End Function


Public Function PrintByteArray(ByVal array() As Byte)

    Dim hex_value As String = ""


    Dim i As Integer
    For i = 0 To array.Length - 1


        hex_value += array(i).ToString("X2")

    Next i


    Return hex_value.ToLower

End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    FolderBrowserDialog1.ShowDialog()
    TextBox2.Text = FolderBrowserDialog1.SelectedPath
    For Each Dir As String In Directory.GetDirectories(FolderBrowserDialog1.SelectedPath)
        ListBox2.Items.Add(Dir)
    Next

End Sub
Public Function Main()
    ' Make a reference to a directory.
    Dim di As New DirectoryInfo(FolderBrowserDialog1.SelectedPath)
    ' Get a reference to each file in that directory.
    Dim fiArr As FileInfo() = di.GetFiles()
    ' Display the names of the files.
    Dim fri As FileInfo
    For Each fri In fiArr
        ListBox1.Items.Add(fri.Name)
    Next
End Function 'Main
Public Function Main2()
    If ListBox2.SelectedItem = Nothing Then
        MsgBox("Please Select A Directory From the List...")

    Else
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo(ListBox2.SelectedItem.ToString)
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names of the files.
        Dim fri As FileInfo
        For Each fri In fiArr
            ListBox1.Items.Add(ListBox2.SelectedItem & "/" & fri.Name)
        Next
        TextBox1.Text = ListBox1.Items.Count.ToString()
    End If
End Function 'Main

Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles Button2.Click
    TextBox2.Text = FolderBrowserDialog1.SelectedPath
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Main()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Main2()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Button2.Image = My.Resources._2_Documents_icon
    Button2.BackgroundImageLayout = ImageLayout.Zoom
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click



    Dim path As String = ListBox1.SelectedItem.ToString

    TB_path.Text = path


    LB_md5.Text = md5_hash(path)
    LB_sha1.Text = sha_1(path)
    LB_sha256.Text = sha_256(path)
    ListBox3.Items.Add(LB_md5.Text)
    'ListBox3.Items.Add(ListBox1.SelectedItem.ToString)
    ListBox3.SelectedIndex += 1
    Dim itemmd5 = LB_sha256.Text





    Dim sb As New System.Text.StringBuilder()

    For Each o As Object In ListBox3.Items
        sb.AppendLine(o)
    Next



    'System.IO.File.WriteAllText("c:\checkedfilesmd5.txt", sb.ToString())
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    Timer1.Start()


End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    If (ListBox1.SelectedIndex = ListBox1.SelectedIndex) Then
        ListBox1.SelectedIndex = 0





        Timer1.Stop()
    Else
        ListBox1.SelectedIndex += 1
        gethash()
    End If
End Sub
Public Sub gethash()
    str = str & " " & ListBox1.SelectedItems(i).ToString



    'My.Settings.itemsmd5 = itemmd5
    Dim path As String = ListBox1.SelectedItem

    TB_path.Text = path


    LB_md5.Text = md5_hash(path)
    LB_sha1.Text = sha_1(path)
    LB_sha256.Text = sha_256(path)
    ListBox3.Items.Add(LB_md5.Text)
    'ListBox3.Items.Add(str)
    ListBox3.SelectedIndex += 1
    Dim itemmd5 = LB_sha256.Text





    Dim sb As New System.Text.StringBuilder()

    For Each o As Object In ListBox3.Items
        sb.AppendLine(o)
    Next

    md5hashindexer += 1
End Sub

Private Sub ShowWindowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowWindowToolStripMenuItem.Click
    Me.Show()
End Sub

Private Sub nfi_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles nfi.MouseDoubleClick
    Me.Show()
    Me.BringToFront()
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    'First minimize the form
    Me.WindowState = FormWindowState.Minimized

    'Now make it invisible (make it look like it went into the system tray)
    Me.Visible = False

    'Cancel the closing of the application
    'e.Cancel = True

    nfi.Visible = True

    MsgBox("Md Antivirus rogram has been minimized to the task bar.")
End Sub
End Class

1 answer

3


You don’t need all that code, just the troublesome part that has to do with it.

In C# or VB.net, indexes start at 0, so the last position is equal to the number of items - 1.

Example:

Item:    1, 2, 3, 4, 5, 6
Índice:  0, 1, 2, 3, 4, 5

In this situation, the Count property will be 6, but the maximum index, 5. So your if will never enter the true condition:

if (ListBox1.SelectedIndex >= ListBox1.Items.Count)

So, enter Else and increment Selectedindex, which can only go up to 5. In an attempt to go up to 6, the error happens.

The comparison should be:

if (ListBox1.SelectedIndex == ListBox1.Items.Count-1) //Se está no último item
  • Thanks for the help in fact I had guessed why of the mistake but had not yet seen in that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.