(VB.Net) 防止将重复项添加到列表框中
(VB.Net) Prevent Duplicates from being added to a list box
我在捕获我正在处理的作业中的重复项时遇到了一些问题。
任务是田径比赛经理。从文本文件中读取时间,然后为每次从文本文件加载的时间输入号码布(又名,无论时间文本文件中有多少行)
号码布号码和时间将按照输入的顺序进行同步。要求是必须使用输入框一次输入一个号码布。每次输入号码布时,它都会加载到名为 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 循环来检查每个列表框项目是否重复,我只是不确定这个循环相对于上面的代码会去哪里。
非常感谢任何帮助。谢谢。
根据Steven B在评论中的建议,您可以这样查看:
If Not listTimeEntry.Items.Contains(itemToBeInserted) Then
listTimeEntry.Items.Add(itemToBeInserted)
End If
不要对不例外的事情使用异常处理。 Exceptionsl 事物是我们无法控制的事物,例如不可用的网络连接。未能输入正确输入的用户一点也不例外。验证输入。不要通过清空列表让用户从头再来,只要让他重新输入上次的输入即可。
我使用 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
我在捕获我正在处理的作业中的重复项时遇到了一些问题。
任务是田径比赛经理。从文本文件中读取时间,然后为每次从文本文件加载的时间输入号码布(又名,无论时间文本文件中有多少行)
号码布号码和时间将按照输入的顺序进行同步。要求是必须使用输入框一次输入一个号码布。每次输入号码布时,它都会加载到名为 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 循环来检查每个列表框项目是否重复,我只是不确定这个循环相对于上面的代码会去哪里。
非常感谢任何帮助。谢谢。
根据Steven B在评论中的建议,您可以这样查看:
If Not listTimeEntry.Items.Contains(itemToBeInserted) Then
listTimeEntry.Items.Add(itemToBeInserted)
End If
不要对不例外的事情使用异常处理。 Exceptionsl 事物是我们无法控制的事物,例如不可用的网络连接。未能输入正确输入的用户一点也不例外。验证输入。不要通过清空列表让用户从头再来,只要让他重新输入上次的输入即可。
我使用 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