检查多个TexBox中的某些文本

问题描述 投票:-1回答:1

我有一个为朋友制作的恶作剧防病毒程序。该软件的一部分需要“激活”才能删除“病毒”。当我单击按钮时,我有4个文本框,我希望检查所有4个TexBoxes的文本“ 0000”。当我有一个TextBox时,它的效果很好,但是在出现消息框之前,我需要对所有4个框进行检查。我希望这是有道理的。 See image here

[[Edit]我要提一下,我在编程时是个菜鸟。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "0000" Then
            MsgBox("Registered")
            Me.Hide()
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class
vb.net textbox
1个回答
1
投票

有很多方法可以做你想做的。这是一个可以构建的非常简单的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' check all the TextBoxes in the array. Return if one isn't valid
    For Each textbox As TextBox In {TextBox1, TextBox2, TextBox3, TextBox4}
        If textbox.Text <> "0000" Then
            Return
        End If
    Next

    ' If all TextBox contains the valid string, this will appear
    MsgBox("Registered")
    Me.Hide()
End Sub

玩得开心!

编辑:

具有4个不同的字符串:只需链接4个检查。像这样:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' If all TextBox contains the valid string, this will appear
    If TextBox1.Text = "0000" AndAlso TextBox2.Text = "1111" AndAlso TextBox3.Text = "2222" AndAlso TextBox4.Text = "3333" Then
        MsgBox("Registered")
        Me.Hide()
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.