How can I compare two listbox items one by one

Asked

Viewed 150 times

2

I’m developing a new software which checks digital signatures and has 2 ListBox. The first with the hash from the files contained in the Directory chosen by the user, the second with the signatures MD5 of known viruses I need to compare item by item if in listbox4.items.contain(listbox5.items).

How can I do this loop and compare with all existing keys one by one?

Ever since I thank.
I’ve Googled but I can’t find anything that can help me.

This is my code:

Private Sub comparador_Tick(sender As Object, e As EventArgs) Handles comparador.Tick
    For Each hash1 As String In ListBox4.Items
        ListBox4.SelectedIndex = ListBox4.SelectedIndex + 1
        For Each hash2 As String In ListBox5.Items
            ListBox5.SelectedIndex = ListBox5.SelectedIndex + 1
            If ListBox4.Items.Contains(ListBox5.SelectedItem) Then
                MsgBox("Virus Encontrado Em:" & ListBox4.SelectedItem)
            Else

            End If
            If (ListBox4.SelectedIndex = ListBox4.Items.Count - 1) Then
                Return
            End If
            If (ListBox5.SelectedIndex = ListBox5.Items.Count - 1) Then
                Return
            End If
        Next
    Next
End Sub

After making a few attempts I ended up modifying some of my initial code to this: Comparator and a timer

    Private Sub comparador_Tick(sender As Object, e As EventArgs) Handles comparador.Tick
    ListBox4.SelectedIndex = 0
    ListBox5.SelectedIndex = 0
    'ListBox4.SelectedIndex = ListBox4.SelectedIndex + 1
    For Each hashf As String In ListBox5.Items


        If ListBox4.SelectedItem.ToString = ListBox5.SelectedItem.ToString 
Then

            MsgBox("Virus Encontrado Em:" & ListBox4.SelectedItem)
        Else
            ListBox5.SelectedIndex = ListBox5.SelectedIndex + 1
        End If
        ListBox4.SelectedIndex = ListBox4.SelectedIndex + 1
    Next
End Sub

error The list to which this enumerator is linked has been modified. A enumerator can only be used if the list does not change. In fact after trying many things I found a solution

Private Sub comparador_Tick(sender As Object, e As EventArgs) Handles comparador.Tick
    TextBox3.Text = ListBox5.Items.Count
    Dim counter As Integer 'new!

    'ListBox4.SelectedIndex = ListBox4.SelectedIndex + 1
    For Each hashf As String In ListBox5.Items.ToString
        ListBox5.SelectedIndex = ListBox5.SelectedIndex + 1
        counter += 1 'new
        ProgressBar1.Value = (counter * 100) / ListBox5.Items.Count 'new
        If (ListBox5.SelectedIndex = ListBox5.Items.Count - 1) Then

            ListBox5.SelectedIndex = 0
            ListBox4.SelectedIndex = ListBox4.SelectedIndex + 1
        End If
        ListBox4.SelectedIndex = 0
        If ListBox4.SelectedItem.ToString = ListBox5.SelectedItem.ToString Then
            MsgBox("Virus Encontrado Em:" & ListBox4.SelectedItem)
        End If
    Next
End Sub

1 answer

2


If the elements in the ListBox are of the type string, then the code below solves the problem simply (necessary to import System.Linq):

Dim listHash = ListBox1.Items.Cast(Of String).ToList()
Dim listVirus = ListBox2.Items.Cast(Of String).ToList()
Dim listUnion = listHash.Intersect(listVirus).ToList()
Dim strVirusFound As String = String.Empty

For Each strVirus In listUnion
    strVirusFound += string.Format("Virus encontrado em: {0}{1}", strVirus, Environment.NewLine)
Next

MsgBox(strVirusFound)

Browser other questions tagged

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