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

68 Views Asked by At

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.stack.imgur.com/aHjXv.png)](https://i.stack.imgur.com/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.

3

There are 3 best solutions below

0
Mark Stevens On BEST ANSWER

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");
        }
    }
3
Akash Kansara On

try this.

private void WriteData(string text)
{
    if (text == "CRLF")
    {               
        serial.DiscardInBuffer();   // clear "CRLF" from serial port buffer
    }
    else
    {
        OutputTextBox.ScrollToEnd();
        OutputTextBox.AppendText(text);
    }
    serial.BaseStream.Flush();
    OutputTextBox.AppendText(Environment.NewLine);
}
1
prabu naresh On
private void WriteData(string text)
{
    if (text == "CRLF")
    {
        serial.DiscardInBuffer();   // clear "CRLF" from serial port buffer
        serial.BaseStream.Flush();
        OutputTextBox.AppendText("\n");  // Add a single newline for the blank line
    }
    else
    {
        OutputTextBox.ScrollToEnd();
        OutputTextBox.AppendText(text + "\n");  // Add a newline after the received text
        serial.BaseStream.Flush();
    }
}

In this version of the code, I've made two changes:

In the case where the text is "CRLF", I added a single OutputTextBox.AppendText("\n"); line to add a newline character, which will result in a blank line being added to the TextBox.

In the else case, I modified the OutputTextBox.AppendText(text); line to include a + "\n" at the end. This ensures that a newline character is added after the received text, creating a new line for the next entry.

With these changes, your code will insert a blank line when "CRLF" is received and also properly insert new lines for other text entries.