2013-02-22 196 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