472,973 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

Result of RichTextBox.AppendText of too many characters?

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
4 13292
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
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


"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: zbcong | last post by:
hello i put a richtextbox and a button on the winForm.when the button is clicked,it generate several threads to write to the richtextbox,the code snippet as following: private void...
1
by: Eric | last post by:
Using the 1.1 framework. We are using a newly created instance of a RichTextBox Class in our server code to manipulate RTF that is stored in the database. To clarify, we are not using or...
2
by: JonnyT | last post by:
I searched high and low for an answer on how to auto scroll a richtextbox and now I finally have it. Since it took me a while to get a good efficient way of doing it that didn't require focus to...
0
by: steve | last post by:
Hello, I have been working on this piece of code that's supposed to read from a file - transform - preview and finally save the transformed data. I can read the file and with...
4
by: tsahiasher | last post by:
hi, i'm trying to use the RichTextBoxSelectionColor property, but in any combination of command order i try, the text color is not changed. the only time it worked was when i set SelectionStart =...
3
by: michael sorens | last post by:
The documentation for the RichTextBox is sketchy at best. I want to do a very simple task but I cannot find information on this. I am using a RichTextBox as an output window. Some text I want to...
0
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a scrolling RichTextBox to which I add characters one at a time using the AppendText method as they are received by my application. Some of these characters may be backspace...
1
by: bmerlover | last post by:
I'm having trouble reading quotations inside the richTextBox when a file is opened on to it. I've tried a couple different methods, most of them didn't compile. I came across one that does compile...
0
by: rwf_20 | last post by:
I've got a form that contains a RichTextBox control and periodically receives strings to append to it. The function in question is below. I've been receiving mysterious bug reports of application...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.