By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Result of RichTextBox.AppendText of too many characters?

P: n/a
Hello,

I have a multiline RichTextBox that I use for data logging. I'm concerned
about how the AppendText method reacts when over time the maximum number of
characters have been added. Do the oldest characters it contains start
rolling off the top into the bit bucket, does it simply start silently
ignoring appends until some characters, are deleted, or does some kind of
exception get thrown? I've heard numerous opinions on what the maximum
character count can be and I can implement an algorithm to deal with this
issue if necessary. But if nothing "bad" happens, I'd prefer not to
introduce the extra complexity.

Thanks,
Ray
Aug 18 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Aug 18, 2:40*pm, Ray Mitchell
<RayMitchell_NOSP...@MeanOldTeacher.comwrote:
Hello,

I have a multiline RichTextBox that I use for data logging. *I'm concerned
about how the AppendText method reacts when over time the maximum number of
characters have been added. *Do the oldest characters it contains start
rolling off the top into the bit bucket, does it simply start silently
ignoring appends until some characters, are deleted, or does some kind of
exception get thrown? *I've heard numerous opinions on what the maximum
character count can be and I can implement an algorithm to deal with this
issue if necessary. *But if nothing "bad" happens, I'd prefer not to
introduce the extra complexity.

Thanks,
Ray
I think you should write a event that would be fired when RichTextBox
size goes beyond the expected size. If you do not want to write a
separate control, in the text changed event, keep checking the size of
the RichTextBox.

In this way you can delete the first bunch of text to acomidate the
new text for log.

Try the following code snippet

const int DEFINEDLENGTH = 100;
const string LOGFILESPATH = @"C:\1.txt";

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Text.ToString().Length DEFINEDLENGTH)
{
richTextBox1.SaveFile(LOGFILESPATH);
richTextBox1.Text = "";
}
}

-Cnu.

Aug 18 '08 #2

P: n/a
On Mon, 18 Aug 2008 02:40:19 -0700, Ray Mitchell
<Ra*****************@meanoldteacher.comwrote:
Hello,

I have a multiline RichTextBox that I use for data logging. I'm
concerned
about how the AppendText method reacts when over time the maximum number
of
characters have been added. Do the oldest characters it contains start
rolling off the top into the bit bucket, does it simply start silently
ignoring appends until some characters, are deleted, or does some kind of
exception get thrown?
Neither TextBox nor RichTextBox have any sort of automatic discarding of
data. When it's full, you'll get an exception. According to the
documentation, the default defined maximum length (MaxLength property) for
RichTextBox is Int32.MaxValue (about 2 billion), but you will run out of
memory before you reach that limit (so the practical limit is actually how
much virtual address space is available).

The default for TextBox is 32767, but you can set that to any arbitrary
value as well, and would be similarly limited to available memory if you
set it large enough.

Some thoughts:

-- Do you really need the rich text features of RichTextBox? There
will be somewhat higher overhead when using that, because of the extra
data requirements for dealing with the formatting. You can probably
squeeze a little more capacity out of the control by using TextBox instead.

-- Performance is likely to suffer before you reach whatever the hard
memory allocation limit is, especially if your computer has limited
physical RAM (say, 2GB or less). The overhead of managing the data, and
especially any disk swapping that occurs, will slow things down.

-- Finally, as it happens I just posted a very simple
text-logging/display control, as a sort of "getting used to blogging"
exercise. It's very limited: it is single-font, has no clipboard support,
nor even a way to get the text back out of the control once you've put it
in. But, it _does_ provide a reasonably efficient implementation of a
control that displays a fixed number of lines of text, automatically
discarding older lines once the total number of lines is reached.

You can find the code for the control here:
http://msmvps.com/blogs/duniho/pages...onsole-cs.aspx

And you can find my discussion of the details of the control (such as they
are) here:
http://msmvps.com/blogs/duniho/archi...ut-part-1.aspx

Pete
Aug 18 '08 #3

P: n/a


"Peter Duniho" wrote:
On Mon, 18 Aug 2008 02:40:19 -0700, Ray Mitchell
<Ra*****************@meanoldteacher.comwrote:
Hello,

I have a multiline RichTextBox that I use for data logging. I'm
concerned
about how the AppendText method reacts when over time the maximum number
of
characters have been added. Do the oldest characters it contains start
rolling off the top into the bit bucket, does it simply start silently
ignoring appends until some characters, are deleted, or does some kind of
exception get thrown?

Neither TextBox nor RichTextBox have any sort of automatic discarding of
data. When it's full, you'll get an exception. According to the
documentation, the default defined maximum length (MaxLength property) for
RichTextBox is Int32.MaxValue (about 2 billion), but you will run out of
memory before you reach that limit (so the practical limit is actually how
much virtual address space is available).

The default for TextBox is 32767, but you can set that to any arbitrary
value as well, and would be similarly limited to available memory if you
set it large enough.

Some thoughts:

-- Do you really need the rich text features of RichTextBox? There
will be somewhat higher overhead when using that, because of the extra
data requirements for dealing with the formatting. You can probably
squeeze a little more capacity out of the control by using TextBox instead.

-- Performance is likely to suffer before you reach whatever the hard
memory allocation limit is, especially if your computer has limited
physical RAM (say, 2GB or less). The overhead of managing the data, and
especially any disk swapping that occurs, will slow things down.

-- Finally, as it happens I just posted a very simple
text-logging/display control, as a sort of "getting used to blogging"
exercise. It's very limited: it is single-font, has no clipboard support,
nor even a way to get the text back out of the control once you've put it
in. But, it _does_ provide a reasonably efficient implementation of a
control that displays a fixed number of lines of text, automatically
discarding older lines once the total number of lines is reached.

You can find the code for the control here:
http://msmvps.com/blogs/duniho/pages...onsole-cs.aspx

And you can find my discussion of the details of the control (such as they
are) here:
http://msmvps.com/blogs/duniho/archi...ut-part-1.aspx

Pete
Hi Pete,

I need a RichTextBox because I color-code my messages. The problem I've had
with simply removing lines of text from the beginning of the string that the
control uses for displaying is that the colors get all messed up. I'm not
sure where the color information is kept but it is obviously not in the
strings themselves. I tried a different approach where I created a
"DisplayLine" class that contains both a string and a color. I created an
array of these with one element for each line I want the control to be able
to display. Then each time a new line comes in I simply move everything in
this array up by one element, put the new line in the last element, clear the
control, re-append all the strings in my DisplayLine object to the control
(first setting the desired color for each), then scroll to the caret. It
only has one basic problem (other than being slow) and that is the flickering
dirves you crazy. Of course I've fought this flickering issue with every
application like this I've ever done and never have solved it (although I'm
sure the solution must be simple). I have set the DoubleBuffered property to
true, but that has never had any effect whatsoever in any of these
applications. I've looked into of just rewiting the visible area of the
control rather than actually letting it redraw line-at-a-time as the whole
set is re-appended, but I'm not proficient enough at this stuff to actually
pull that off yet. So my applications just flicker away! Suggestions are
welcome.

Ray
Aug 20 '08 #4

P: n/a
On Wed, 20 Aug 2008 02:48:01 -0700, Ray Mitchell
<Ra*****************@meanoldteacher.comwrote:
[...]
I need a RichTextBox because I color-code my messages.
Ah...this sounds familiar. I think we've talked about this problem with
you before, right? :)
[...] It
only has one basic problem (other than being slow) and that is the
flickering
dirves you crazy. Of course I've fought this flickering issue with every
application like this I've ever done and never have solved it (although
I'm
sure the solution must be simple). I have set the DoubleBuffered
property to
true, but that has never had any effect whatsoever in any of these
applications.
Right. DoubleBuffered addresses a particular kind of "flickering". That
is, the erasing of the background before redrawing the new contents of the
control or other kinds of "compositing" operations. But if all that's
happening is that the control has to keep redrawing itself because it is
repeatedly changing, some other technique would be needed (such as somehow
disabling updates altogether until what's considered a single change has
been completed, even if that single change involves multiple logical
changes).
I've looked into of just rewiting the visible area of the
control rather than actually letting it redraw line-at-a-time as the
whole
set is re-appended, but I'm not proficient enough at this stuff to
actually
pull that off yet. So my applications just flicker away! Suggestions
are
welcome.
Well, the code I directed you to could trivially be changed to support
per-line coloring. I've already got a "DisplayLine" data structure in
there that tracks the line information. Right now the only
client-provided piece of information is the string itself, but you could
easily add a Color to that too (passing it in to the Append() or
AppendLine() methods), and then create the Brush used for drawing each
line of text using the Color.

Alternatively, you might consider using the SelectionStart,
SelectionLength, and SelectedText properties to remove text at the
beginning of the control rather than doing the multi-step process. Just
set the SelectionLength and SelectedText values so that the selection
contains exactly the text you want removed, and then set SelectedText to
String.Empty.

Of course, you could just not worry about removing text at the beginning
of the control. Without knowing your specific needs, it's hard to know
whether that's appropriate or not. But the theoretical maximum content of
the RichTextBox control is quite large, and if you know for sure you'll
never come close to that, maybe the best solution is to just not worry
about it. :)

Pete
Aug 20 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.