0

在我正在处理的作业中捕获重复项时遇到了一些麻烦。

任务是田径比赛经理。从文本文件中读取时间,然后为从文本文件加载的每次时间输入一个围兜号码(也就是时间文本文件中有多少行)

然后,号码布号码和时间会按照输入的顺序进行同步。要求是必须使用输入框一次输入一个号码布号码。每次输入书目号码时,都会将其加载到名为 lstBibs 的列表框中。

我在使用输入框方面的经验有限,并且到目前为止 我为检查重复项所做的任何尝试都在运行时被忽略了。我也不确定我会在哪里放置一个循环来检查输入框中的重复项。以下是我到目前为止的代码。

 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

所以我假设我必须使用一个 for 循环来检查每个列表框项是否重复,我只是不确定这个循环相对于上面的代码会去哪里。

非常感谢任何和所有帮助。谢谢你。

4

2 回答 2

0

根据评论中的 Steven B 建议,您可以这样检查:

If Not listTimeEntry.Items.Contains(itemToBeInserted) Then 
     listTimeEntry.Items.Add(itemToBeInserted)
End If
于 2019-05-03T15:32:55.240 回答
0

不要对不异常的事情使用异常处理。例外情况是我们无法控制的事情,例如不可用的网络连接。未能输入正确输入的用户一点也不例外。验证输入。不要通过清除列表让用户重新开始,只需要求他重新输入最后一次输入即可。

我使用 TryParse 验证用户输入。如果第一个条件成功,则检查 AndAlso 条件。如果 TryParse 失败,则永远不会评估 AndAlso 条件,因此我们不会得到异常。第二个条件是检查该号码是否已被使用。只有当这两个条件都通过时,我们才会将数字添加到已用数字列表中,更新 lstBibs 并增加计数。

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
于 2019-05-04T02:15:36.347 回答