R
Ray Mitchell
Hello,
I am using a RichTextBox to display many lines of text, where the characters
are of various colors. To keep the text box from getting fuller than I would
like, each time it reaches a specific number of lines I copy its contents out
to a string array, copy only the last "n" lines of that array into another
string array, then reassign that new smaller array into the text box. As a
result the text box then only displays the newer lines. For example:
int linesInBox = richTextBox0.Lines.Length;
if (linesInBox > MAX_LINES_WANTED_IN_BOX)
{
// Get all lines from the rich text box
string[] currentLines = richTextBox0.Lines;
// Copy only newer lines from current text box into a new array (starting
from an offset).
string[] newLines = new string[currentLines.Length - TEXT_BOX_LINE_OFFSET];
for (int ix = 0, ixt = TEXT_BOX_LINE_OFFSET; ixt < linesInBox; ++ix,
++ixt)
newLines[ix] = currentLines[ixt];
richTextBox.Lines = newLines;
}
The problem is that the text copied back into the text box no longer has the
various colors (or any other special attributes). This is obviously because
the attributes are associated with the control itself and not with the text
itself. So, is there an easy way to do what I am trying to do that will also
preserve the attributes of each character? A solution I thought of, but I
hate to resort to actually doing it, is that as I place each original
character into the text box, I also place a copy of it into a secondary array
along with its attributes. Then, when it's time to rewrite the text box, I
simply clear it first then individually append each character from my
secondary array along with the appropriate attributes. There must be an
easier way, however.
Thanks,
Ray
I am using a RichTextBox to display many lines of text, where the characters
are of various colors. To keep the text box from getting fuller than I would
like, each time it reaches a specific number of lines I copy its contents out
to a string array, copy only the last "n" lines of that array into another
string array, then reassign that new smaller array into the text box. As a
result the text box then only displays the newer lines. For example:
int linesInBox = richTextBox0.Lines.Length;
if (linesInBox > MAX_LINES_WANTED_IN_BOX)
{
// Get all lines from the rich text box
string[] currentLines = richTextBox0.Lines;
// Copy only newer lines from current text box into a new array (starting
from an offset).
string[] newLines = new string[currentLines.Length - TEXT_BOX_LINE_OFFSET];
for (int ix = 0, ixt = TEXT_BOX_LINE_OFFSET; ixt < linesInBox; ++ix,
++ixt)
newLines[ix] = currentLines[ixt];
richTextBox.Lines = newLines;
}
The problem is that the text copied back into the text box no longer has the
various colors (or any other special attributes). This is obviously because
the attributes are associated with the control itself and not with the text
itself. So, is there an easy way to do what I am trying to do that will also
preserve the attributes of each character? A solution I thought of, but I
hate to resort to actually doing it, is that as I place each original
character into the text box, I also place a copy of it into a secondary array
along with its attributes. Then, when it's time to rewrite the text box, I
simply clear it first then individually append each character from my
secondary array along with the appropriate attributes. There must be an
easier way, however.
Thanks,
Ray