Search code examples
vb.netwinformstextrichtextbox

How to verify the string value from a richtextbox


Original Code:

The code below always runs Case Else because the text values have an exclamation in them. It I were to remove them, then they would work again.

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

Edit

Trying the suggested method (seen below) also only runs 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

Solution

  • So I figured it out by looping through all Char's in the string. Apparently Environment.NewLine, vbcr, or something similar gets added to the end of the string. To resolve this I used the following code:

        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