如何将换行符放入文本框中,该换行符仅在收到字符串“CRLF”时发生

问题描述 投票:0回答:2

如何将换行符放入文本框中,该换行符仅在收到字符串“CRLF”时发生

这是我的文本框输出

显示 CRLF?J] 的行应该只是一个空行。

这是我正在使用的代码。

    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");
        }
    }

感谢所有帮助。

c# textbox code-behind
2个回答
0
投票

试试这个。

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);
}

0
投票
private void WriteData(string text)
    {
/*
I Think before you seek help,
You must specify the content of the text variable
If the content of the text variable is a raw string like 
U1_1A_AA
01110101
CRLF?J]
...
Then you can replace CRLF with the help of String.replace(string oldValue, string newValue)
for example
text.replace("CRLF", "\n\n")
or
if(text.StartsWith(string value)) {

}
*/
        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");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.