【问题标题】:How to verify the string value from a richtextbox如何验证来自富文本框的字符串值
【发布时间】:2021-07-15 05:49:24
【问题描述】:

原代码:

下面的代码总是运行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

【问题讨论】:

  • 是的,我想检查字符串是否以除区分大小写之外的所有方式匹配。如果我必须使用区分大小写的验证方法,那就是找到原因,我只需 ToLower 就可以绕过它。
  • 如果你想要一个不区分大小写的匹配,你可以保持当前的形式并改变,例如,[...] Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase) [...]——我不知道是什么检查字符串是否以各种方式匹配除了区分大小写意味着,在实践中。您必须在问题中添加 - 而不是在 cmets 中 - 更好地描述您的意图。
  • 对不起,我不知道该怎么解释。我对编程很陌生,而且我是自学成才的,所以我很难很好地解释一些事情。我将投票删除该帖子,因为这已经变得比它的价值更成问题。不幸的是,Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase) 也不起作用。不过谢谢你的帮助。我会在以后的问题中尝试更好地解释。
  • 如果要测试Text是否包含字符串,可以使用,例如Case DeveloperCommandLine.Text.IndexOf("!exit", StringComparison.CurrentCultureIgnoreCase) >= 0
  • 只是一个问题,你正在尝试匹配“!exit”,那个解释标记不应该做一些逻辑否定吗?例如 DeveloperCommandLine.Text 不等于退出

标签: vb.net winforms text richtextbox


【解决方案1】:

试试这个而不是 Select Case:

    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

【讨论】:

  • 这行得通,但自然而然,即使它们只是文本的子字符串,它也会起作用。
【解决方案2】:

所以我通过遍历字符串中的所有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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多