【发布时间】:2019-05-03 02:26:51
【问题描述】:
我在处理我正在处理的作业中发现重复项时遇到了一些麻烦。
分配的是一名田径比赛经理。从文本文件中读取时间,然后为从文本文件中加载的每次时间输入一个围兜号码(也就是时间文本文件中有多少行)
然后,号码布号码和时间会按照输入的顺序进行同步。要求是必须使用输入框一次输入一个号码布号码。每次输入号码布时,它都会加载到一个名为 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 循环来检查每个列表框项是否重复,我只是不确定这个循环相对于上面的代码会去哪里。
非常感谢任何和所有的帮助。谢谢。
【问题讨论】:
-
已经有一段时间了,但我相信
listTimeEntry.Items将有一个.contains()方法来检查项目的出现。 -
将字符串添加到
HashSet(Of String),使用[HashSet].Add(string)添加字符串(不会添加重复项),然后使用ListBox1.DataSource = [HashSet].ToString()填充ListBox -
使用字典,key就是你的值..
-
请开启 Option Strict。这是一个两部分的过程。首先对于当前项目 - 在解决方案资源管理器中双击我的项目。选择左侧的编译。在 Option Strict 下拉列表中选择 ON。未来项目的第二个 - 转到工具菜单 -> 选项 -> 项目和解决方案 -> VB 默认值。在 Option Strict 下拉列表中选择 ON。这将使您避免在运行时出现错误。
标签: vb.net duplicates inputbox