开发者

RichTextBox not processing new line/carriage return characters

开发者 https://www.devze.com 2023-03-18 23:16 出处:网络
I seem to be experiencing a very unusual issue with the RichTextBox control, programming in C# in Visual Studio 2008.

I seem to be experiencing a very unusual issue with the RichTextBox control, programming in C# in Visual Studio 2008.

I'm currently reading the s开发者_高级运维tream of a file that's being written to by the windows console (with output redirection), and I'm reading it into my program (essentially, I'm recreating what's known in the unix world as "tail").

When an array of bytes is read in, I'd like to return to a new line line, and continue reading. However, it seems like my RTB will not respond to new line or carriage return characters. For example, I've tried appending \n, \r\n, and even Environment.NewLine to the string being written, but nothing seems to be working. It just keeps writing across the same line.

Here is the code that seems to be causing trouble:

string convertedBuffer = System.Text.Encoding.UTF8.GetString((byte[])e.UserState);

outputBox.AppendText(convertedBuffer + "\n");

I'm really at a loss here, can anyone help?


Its probably because you set outputBox.Multiline property to false which will will prevents the control from showing the text in multi-line.

Edit: You stated at comment

the byte array that is being sent was one that I made of set size, 512. I now just changed that to one that actually fits the data being filled in it, and now, all of a sudden, the newline character starts working again

Here is what happened:

The string class in C# is an array of char start by pointer to the first char in the array and ends by the special "terminal" char \0. So when you call System.Text.Encoding.UTF8.GetString(buffer) if the buffer size is larger than the string size it will set the all remaining chars of the string to the terminal char in the char array. the array of chars will be something like some text\0\0\0, So I am guessing that when you add Environment.NewLine it will add it to the last index of the char array after last \0 and then the char array will be some text\0\0\r\n and because of the new line is after the terminal it will ignore it.


Just put "\n" or Environment.NewLine separately as follows:

outputBox.AppendText(convertedBuffer);
outputBox.AppendText("\n");

It works for me... and you don't have to worry about \0

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号