如何验证 richtextbox 中的字符串值

How to verify the string value from a richtextbox

原码:

下面的代码始终运行 Case Else,因为文本值中包含感叹号。如果我将它们移除,它们就会再次工作。

Private Sub CommandLineFunctions()
    Select Case True
        Case DeveloperCommandLine.Text = "!exit"
            ToolStripButtonClose.PerformClick()
        Case DeveloperCommandLine.Text = "!clear"
            MessageDisplayBounds.Text = String.Empty
        Case DeveloperCommandLine.Text = "!disable"
            DeveloperMode.Enabled = False
            CloseForm(ToolStripButtonClose, EventArgs.Empty)
        Case Else
            MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
            ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
    End Select
    DeveloperCommandLine.Text = String.Empty
End Sub

编辑

尝试建议的方法(见下文)也只能运行 Case Else

Private Sub CommandLineFunctions()
    Select Case True
        Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase)
            ToolStripButtonClose.PerformClick()
        Case DeveloperCommandLine.Text.Equals("!clear", StringComparison.CurrentCultureIgnoreCase)
            MessageDisplayBounds.Text = String.Empty
        Case DeveloperCommandLine.Text.Equals("!disable", StringComparison.CurrentCultureIgnoreCase)
            DeveloperMode.Enabled = False
            CloseForm(ToolStripButtonClose, EventArgs.Empty)
        Case Else
            MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
            ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
    End Select
    DeveloperCommandLine.Text = String.Empty
End Sub

试试这个而不是 Select 案例:

    If DeveloperCommandLine.Text.ToLower().Contains("exit") Then
        ToolStripButtonClose.PerformClick()
    ElseIf DeveloperCommandLine.Text.ToLower().Contains("clear") Then
        MessageDisplayBounds.Text = String.Empty
    ElseIf DeveloperCommandLine.Text.ToLower().Contains("disable") Then
        DeveloperMode.Enabled = False
        CloseForm(ToolStripButtonClose, EventArgs.Empty)
    Else
        MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
        ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
    End If

所以我通过遍历字符串中的所有 Char 来解决这个问题。显然 Environment.NewLinevbcr 或类似的东西被添加到字符串的末尾。为了解决这个问题,我使用了以下代码:

    Private Sub CommandLineFunctions()
        Dim GetChars As New List(Of String)
        For Each i As Char In DeveloperCommandLine.Text
            GetChars.Add(i)
        Next
        GetChars.Remove(GetChars.Last)
        Dim CleanInput As String = String.Join("", GetChars)
        Select Case True
            Case CleanInput = "!exit"
                ToolStripButtonClose.PerformClick()
            Case CleanInput = "!clear"
                MessageDisplayBounds.Text = String.Empty
            Case CleanInput = "!disable"
                DeveloperMode.Enabled = False
                CloseForm(ToolStripButtonClose, EventArgs.Empty)
            Case Else
                MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
                ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
        End Select
        DeveloperCommandLine.Text = String.Empty
    End Sub