2013-02-22 147 views
3

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

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 
+0

'如果pins.IndexOf(textBox2.Text)< > -1 Then ... End If'(假设我记得VB正确。)ps另见:http://stackoverflow.com/a/11112305/298053 – 2013-02-22 20:16:28

回答

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 

但是,如果必须使用一个数组,可以使用IndexOf方法在Array类:

If Array.IndexOf(TextBox2.Text) >=0 Then 
    TextBox1.Text = "Success" 
End If 
0
If Array.IndexOf(pins, TextBox2.Text) <> -1 Then 
    TextBox1.Text = "Succes" 
End If End Sub 
0
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