简体   繁体   中英

Issue Preventing Duplicates from Being Added to a Listbox When Listbox Contains Items Loaded from a Save File

I am experiencing issues preventing duplicates from being added to a listbox.

  • My Windows Form has 2 listboxes
  • The form is designed so that when the user clicks a button, the items they have checked in first box are added to the second box
  • The code below that I have added to the "add" button is intended to prevent the checked item in box 1 from being added to box 2 IF an identical item already exists in box 2.
  • The problem with my code is that it is not stopping a duplicate from being added to box 2 if box 2 contains items that were loaded from a save file.

Any thoughts on how to fix this issue?

    Public Sub additems_Click(sender As Object, e As EventArgs) Handles additems.Click

    Dim itemChecked As Object
    Dim alreadyonkey As Boolean
    Dim duplicates As Integer = 0       

   If box1.CheckedItems.Count > 0 Then
        For Each itemChecked In box1.CheckedItems
            alreadyadded = False
            'Check if item selected has already been added to box2
            If box2.Items.Contains(itemChecked) = True Then
                alreadyadded = True
                duplicates = duplicates + 1
            Else
                alreadyadded = False
            End If

            'Add item if all criteria met
            If box2.Items IsNot "" And alreadyadded = False Then
                box2.Items.Add(itemChecked)
            End If
        Next

        If duplicates > 0 Then
            MsgBox("One or more of the items you are trying to add have already been added.", MsgBoxStyle.Critical, "Item has already been added")
            alreadyadded = False
        End If
    End If

End Sub

I figured out the issue with my code... The issue was primarily due to the fact that a nested For Each loop needed to be used to compare each item in box1 to each item in box2 one at a time and the items needed to be sent to string variables and them compared using "String.Equals".

Public Sub additems_Click(sender As Object, e As EventArgs) Handles additems.Click

    Dim itemChecked As Object
    Dim alreadyadded As Boolean
    Dim duplicates As Integer = 0


    If box1.CheckedItems.Count > 0 
        For Each itemChecked In box1.CheckedItems
            Dim itemtoadd As String = itemChecked.ToString

            'Check if item selected has already been added to box2
            For Each item In box2.Items
                Dim box2item As String = item.ToString

                If String.Equals(Trim(itemtoadd), Trim(box2item)) = True Then
                    alreadyadded = True
                    duplicates = duplicates + 1
                Else

                End If
            Next

            'Add item if all criteria met
            If itemChecked IsNot "" And alreadyadded = False Then
                box2.Items.Add(itemChecked)
            End If
        Next


        If duplicates > 0 Then
            MsgBox("One or more of the items you are trying to add have already been added.", MsgBoxStyle.Critical, "Item hase already been added")
            alreadyadded = False
            duplicates = 0
        End If
    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM