vb.net - 检查 textbox.text 是否存在于数组中

标签 vb.net visual-studio textbox

这里我有我的简单代码,包含数组列表和 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

最佳答案

如果你想使用 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

关于vb.net - 检查 textbox.text 是否存在于数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15032446/

相关文章:

c# - 使用计时器处理类的正确方法是什么?

vb.net - 在运行时从主应用程序中的类库更改连接字符串

vb.net - 检查应用程序是 mvc、WebForms 还是两者兼而有之

c# - 如何在使用 '.NETFramework,Version=v4.5.2' 的项目中安装 System.Drawing.Common?

python - 如何在 tkinter 中通过本地或网络打印机进行打印

ASP.NET, VB : checking which items of a CheckBoxList are selected

visual-studio - 如何使一个 Visual Studio 项目在另一个项目之前自动执行?

visual-studio - vcproj/vsprops 的可选环境变量

.net - 如何监视 winforms TextBox.SelectionStart 属性的更改?

c# - 当 Visible = False 时,TextBox TextChanged 事件不会触发?