【问题标题】:Check if textbox.text exist in array检查 textbox.text 是否存在于数组中
【发布时间】:2013-02-08 13:24:42
【问题描述】:

这里我有我的简单代码,包含数组列表和 2 个文本框,当我按下按钮脚本必须检查是否在数组列表中找到文本表单 Textbox2。你能帮我修一下吗? 谢谢!

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pins() As String = {"dgge", "wada", "caas", "reaa"}
    If TextBox2.Text = pins() Then
        TextBox1.Text = "Succes"
    End If End Sub

【问题讨论】:

标签: vb.net visual-studio textbox


【解决方案1】:

如果你想使用 LINQ,你可以这样做:

If pins.Contains(TextBox2.Text) Then
    TextBox1.Text = "Success"
End If

否则,最简单的选择是使用 List 而不是数组:

Dim pins As New List(Of String)(New String() {"dgge", "wada", "caas", "reaa"})
If pins.Contains(TextBox2.Text) Then
    TextBox1.Text = "Success"
End If

但是,如果必须使用数组,可以使用Array 类中的IndexOf 方法:

If Array.IndexOf(TextBox2.Text) >=0 Then
    TextBox1.Text = "Success"
End If

【讨论】:

    【解决方案2】:
    If Array.IndexOf(pins, TextBox2.Text) <> -1 Then
        TextBox1.Text = "Succes"
    End If End Sub
    

    【讨论】:

      【解决方案3】:
      If pins.IndexOf(TextBox2.Text) >= 0 Then
          TextBox1.Text = "Founded"
      End If
      

      或者如果你使用List(Of String)而不是数组:

      If pins.Contains(TextBox2.Text) Then
          TextBox1.Text = "Founded"
      End If
      

      【讨论】:

        猜你喜欢
        • 2013-10-03
        • 1970-01-01
        • 1970-01-01
        • 2016-10-22
        • 2013-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多