【问题标题】:(VB.Net) Prevent Duplicates from being added to a list box(VB.Net) 防止将重复项添加到列表框中
【发布时间】: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


【解决方案1】:

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

我使用 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

【讨论】:

  • 再次您好,再次感谢玛丽!你似乎是 VB.net 的守护天使 :)
【解决方案2】:

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

If Not listTimeEntry.Items.Contains(itemToBeInserted) Then 
     listTimeEntry.Items.Add(itemToBeInserted)
End If

【讨论】:

  • 嗨,谢谢,但我已经尝试过了。从输入框输入时似乎没有注册。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多