How can I compare two listbox items one by one?

1

I am developing a new software that verifies digital signatures and I have 2 ListBox . The first one with the hash that is obtained from the files contained in the directory chosen by the user, the second with the known% signatures of% virus I need to compare item by item if in MD5 .

How can I make this listbox4.items.contain(listbox5.items) and compare with all existing keys one by one?

Thank you in advance.
I already searched on Google but did not find anything that could 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 for 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. An enumerator can only be used if the list does not change. In fact after trying several 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
    
asked by anonymous 04.08.2018 / 09:02

1 answer

1
If the elements in ListBox are of type string , then the code below solves the problem in a simple way ( need 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)
    
06.08.2018 / 15:50