Search code examples
vb.netduplicatesinputbox

(VB.Net) Prevent Duplicates from being added to a list box


I'm having some trouble with catching duplicates in an assignment I'm working on.

The assignment is a track and field race manager. Times are read from a text file, a bib number is then entered for each time loaded from the text file (aka, however many rows there are in the time text file)

The bib numbers and times are then synced in the order from which they were entered. A requirement is that the Bib numbers must be entered one at a time using an input box. Every time a bib number is entered, it is loaded to a list box called lstBibs.

The Issue I have limited experience working with input boxes, and any attempts Ive made so far to check for duplicates has been ignored in Runtime. I'm also unsure where I would put a loop that checks for duplicates in the input box. Below is my code so far.

 Dim i As Integer = 0
    Dim Bibno As Integer = 0
    Dim strrow As String = ""
    Dim count As Integer = 0
    Dim errorCount1 As Integer = 0

    'Reads through the number of rows in the Time Text File. 
    'The Number of rows we have in the text file corresponds to the number
    'of bib numbers we need. Thus the input box will loop through bib 
    'numbers until
    'we reach the amount of loaded times

    Try

        For Each item In lstTimeEntry.Items

            i += 1

            For Bibno = 1 To i
                count += 1
                Bibno = InputBox("Enter Bib #" & count)
                lstBibs.Items.Add(count & " - " & Bibno)
                btnSyncTimesBibs.Enabled = True

            Next
        Next

    Catch ex As Exception

        'Catches any invalid data that isnt a number
        MsgBox("Invalid Input, Please Try Again", , "Error")
        lstBibs.Items.Clear()
        btnSyncTimesBibs.Enabled = False

    End Try

So I'm assuming that I must use a for loop that checks each list box item for a duplicate, I'm just unsure where this loop would go in relation to the code above.

Any and all help is very much appreciated. Thank you.


Solution

  • Don't use exception handling for something that isn't exceptional. Exceptionsl things are things beyond our control like a network connection that isn't available. A user failing to enter proper input is not at all exceptional. Validate the input. Don't make the user start all over again by clearing the list, just ask him to re-enter the last input.

    I validate the user input with a TryParse. If the first condition succeeds then the AndAlso condition is checked. It the TryParse fails then the AndAlso condition is never evaluated so we will not get an exception. The second condition is checking if the number has already been used. Only if both conditions pass do we add the number to the used numbers list, update the lstBibs and increment the count.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim Bibno As Integer
        Dim count As Integer = 1
        Dim usedNumbers As New List(Of Integer)
        Do While count <= lstTimeEntry.Items.Count
            Dim input = InputBox("Enter Bib #" & count)
            If Integer.TryParse(input, Bibno) AndAlso Not usedNumbers.Contains(Bibno) Then
                usedNumbers.Add(Bibno)
                lstBibs.Items.Add(count & " - " & Bibno)
                count += 1
            Else
                MessageBox.Show("Please enter a number that does not appear in the list box")
            End If
        Loop
    End Sub