需要帮助将字符串与带有循环的列表框进行比较

时间:2018-11-30 18:31:40

标签: vb.net

代码在下面。我正在尝试将用户输入的字符串与一个大型列表框进行比较,如果找到匹配项,它将终止循环并打印响应。我一直挂在上面冻结或打印错误的响应。

judge
judge
judge
dev
dev

1 个答案:

答案 0 :(得分:3)

您需要增加“ i”

<urlMappings>
  <add url="~/account/" mappedUrl="~/account/default.aspx"/>
</urlMappings>

或者更好,使用for循环

Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
        Dim input As String = txtIn.Text
        Dim i As Integer = 0
        While i <= lstRoseBowl.Items.Count - 1
            If input = CStr(lstBox.Items(i)) Then
                txtOut.Text = "Yes"
            Else
                txtOut.Text = "No"
            End If

            i += 1 ' <-----
        End While
    End Sub

现在,它将编译并运行,但可能不会为您提供所需的结果,因为如果它在第一次尝试中找到了项目,那么它将在第二次尝试中显示“否”。有更好的方法,但是对于最少的代码更改,它看起来像这样。

Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
        Dim input As String = txtIn.Text

        For i As Integer = 0 To lstRoseBowl.Items.Count - 1
            If input = CStr(lstBox.Items(i)) Then
                txtOut.Text = "Yes"
            Else
                txtOut.Text = "No"
            End If
        Next
    End Sub