vb.net 检查数组中是否存在 textbox.text
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15032446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if textbox.text exist in array
提问by Malasuerte94
Here i have my simple code, containt list of arrays and 2 text boxes, when i press button script must check if text form Textbox2 is found in list of array. Can you help me to fix it ? Thanks !
这里我有我的简单代码,包含数组列表和 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
采纳答案by Steven Doggart
If you want to use LINQ, you can just do this:
如果你想使用 LINQ,你可以这样做:
If pins.Contains(TextBox2.Text) Then
TextBox1.Text = "Success"
End If
Otherwise, the easiest option would be to use a Listinstead of an array:
否则,最简单的选择是使用 aList而不是数组:
Dim pins As New List(Of String)(New String() {"dgge", "wada", "caas", "reaa"})
If pins.Contains(TextBox2.Text) Then
TextBox1.Text = "Success"
End If
But, if you must use an array, you can use the IndexOfmethod in the Arrayclass:
但是,如果必须使用数组,则可以使用类中的IndexOf方法Array:
If Array.IndexOf(TextBox2.Text) >=0 Then
TextBox1.Text = "Success"
End If
回答by MarcinJuraszek
If Array.IndexOf(pins, TextBox2.Text) <> -1 Then
TextBox1.Text = "Succes"
End If End Sub
回答by SysDragon
If pins.IndexOf(TextBox2.Text) >= 0 Then
TextBox1.Text = "Founded"
End If
Or if you use List(Of String)instead of array:
或者,如果您使用List(Of String)代替数组:
If pins.Contains(TextBox2.Text) Then
TextBox1.Text = "Founded"
End If

