在Visual Basic 2012中使用带有InStr的发誓字过滤器

时间:2015-03-22 14:51:59

标签: vba

我想将IF参数与字符串数组进行比较。用户将尝试将团队名称放入文本框中,如果用户在该文本框中的任何位置使用发誓单词,则会显示错误消息并清除文本框。如果用户没有宣誓,它将注册团队名称并继续执行该程序(如第二次IF声明中所示)。我试图让这段代码工作一周,但无法让它工作。

Private Sub SelectionButtonEnter_Click(sender As Object, e As EventArgs) Handles SelectionButtonEnter.Click
    Dim HasSworn As Boolean = False
    Dim swears() As String = {"Fuck", "fuck", "Shit", "shit", "Shite", "shite", "Dick", "dick", "Pussy", "pussy", "Piss", "piss", "Vagina", "vagina", "Faggot", "faggot"} 'Declare potential swear words the kids can use
    For Each swear As String In swears
        If InStr(SelectionTextBoxTeamName.Text, swear) > 0 Then
            SelectionTextBoxTeamName.Clear() 'Clear the textbox
            MessageBox.Show("Remember ... You can be disqualified, raise your hand and Blair will set up the program for you again") 'Let the user know they have entered a swear word and ask them to select another team name
        End If
        If Not InStr(SelectionTextBoxTeamName.Text, swear) > 0 Then
            Timer1.Enabled = True 'Enable timer 1 for the learn box
            Timer3ForSelection.Enabled = True 'Enable this timer to show the learn button
            TeamName = SelectionTextBoxTeamName.Text() 'Once this button has been pressed, store the content of that textbox in a the TeamName string 
            SelectionLabelTeamName.Text = "Welcome " & SelectionTextBoxTeamName.Text & " Please click 'Learn' in the box below to begin" 'Display the contents of the string along with other text here
            SelectionLabelTeamNameTL.Text() = "Team Name: " & TeamName 'Display the contents of the string along with other text here
            SelectionTextBoxTeamName.BackColor = Color.Green 'Have the back color of the box set to green
            SelectionTextBoxTeamName.Enabled = False 'Do not allow the user/users enter another team name
        End If
    Next 'A next must be declared in a for each statement
End Sub

提前致谢。

1 个答案:

答案 0 :(得分:1)

我不认为我会这样接近它;如果用户键入f ** kyou,您的代码将无法捕获它。怎么样呢:

在您的代码中:

If ContainsBannedWord(SelectionTextBoxTeamName.Text) Then
   Msgbox "Hold out your hand, bad person. SlapSlapSlap"
Else
   Msgbox "Good boy!"
End if

Function ContainsBannedWord(sInput As String) As Boolean

    Dim aBannedWords(1 To 5) As String
    Dim x As Long

    ' Make all the banned words capitalized
    aBannedWords(1) = "BANNED1"
    aBannedWords(2) = "BANNED2"
    aBannedWords(3) = "BANNED3"
    aBannedWords(4) = "BANNED4"
    aBannedWords(5) = "BANNED5"

    For x = LBound(aBannedWords) To UBound(aBannedWords)
        If InStr(UCase(sInput), aBannedWords(x)) > 0 Then
            ContainsBannedWord = True
            Exit Function
        End If
    Next

    ContainsBannedWord = False

End Function