Preserving character attributes in a RichTextBox

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
 
P

Peter Duniho

Ray said:
[...]
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? [...]

You could parse the RTF directly. Retrieve it, remove the lines you
don't want, set the text box RTF text to the new RTF.

Alternatively, you could set the selection to the text you want to
remove, and then set the SelectedText property to "". That should
remove just the selected text, leaving the rest intact.

Pete
 
R

Ray Mitchell

Peter Duniho said:
Ray said:
[...]
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? [...]

You could parse the RTF directly. Retrieve it, remove the lines you
don't want, set the text box RTF text to the new RTF.

Alternatively, you could set the selection to the text you want to
remove, and then set the SelectedText property to "". That should
remove just the selected text, leaving the rest intact.

Pete
.

Thanks Pete,

I tried your second suggestion and it was just what I needed. For some
reason, however, a completely empty string "" has no effect. But a string
with at least one character in it, even if it is just a space, does the trick.

Ray
 
P

Peter Duniho

Ray said:
I tried your second suggestion and it was just what I needed. For some
reason, however, a completely empty string "" has no effect. But a string
with at least one character in it, even if it is just a space, does the trick.

Empty string works fine here. I suspect some mistake in your
implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top