Search code examples
c#textboxcode-behind

How can I put a newline into a TextBox, which only occurs when the string "CRLF" is received


How can I put a newline into a TextBox, which only occurs when the string "CRLF" is received

This is what I am trying to achieve.

enter image description here

This is my textbox output at the moment

[![enter image description here](https://i.sstatic.net/aHjXv.png)](https://i.sstatic.net/aHjXv.png)

The line which shows CRLF?J] should be just a blank line.

This is the code I am using.

    private void WriteData(string text)
    {
        if (text == "CRLF")
        {               
            serial.DiscardInBuffer();   // clear "CRLF" from serial port buffer
            serial.BaseStream.Flush();
            OutputTextBox.AppendText("\n\n");
        }
        else
        {
            OutputTextBox.ScrollToEnd();
            OutputTextBox.AppendText(text);
            serial.BaseStream.Flush();
            OutputTextBox.AppendText("\n");
        }
    }

Any and all help gratefully received.


Solution

  • This is my new code, works fine now, thanks all for contributing @Selvin pointed out the right path with his comment.

        private void WriteData(string text)
        {
            string SerialData = text;
    
    
            if (SerialData.Contains ("CRLF"))
            {
                serial.DiscardInBuffer();   // clear "CRLF" from serial port buffer
                OutputTextBox.AppendText("\n");
            }
            else
            {
                OutputTextBox.ScrollToEnd();
                OutputTextBox.AppendText(text + "\n");
            }
        }