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 stream 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.Multilineproperty to false which will will prevents the control from showing the text in multi-line.Edit: You stated at comment
Here is what happened:
The
stringclass in C# is an array ofcharstart by pointer to the first char in the array and ends by the special “terminal” char\0. So when you callSystem.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 likesome text\0\0\0, So I am guessing that when you addEnvironment.NewLineit will add it to the last index of the char array after last\0and then the char array will besome text\0\0\r\nand because of the new line is after the terminal it will ignore it.