Deleting strings from a RichTextBox string array

G

Guest

One more for today....

As I add more and more lines to my RichTextBox the array that holds its
strings gets bigger and bigger and the vertical scroll bar gets smaller and
smaller until the string array finally runs out of memory. I'd like to set
some line limit and once that limit is reached, start removing the first line
each time a new line is appended onto the end. I think this is probably more
of an array manipulation question than a text box question (but maybe not).
Is there a simple way to remove the first element or do I manually need to
simply rewrite the entire array each time a new line comes in? I also have a
feeling this might cause massive screen flashing, but at least you'll know
it's doing something! Is there a better way?

Thanks,
Ray
 
P

Peter Duniho

[...]
Is there a simple way to remove the first element or do I manually need
to
simply rewrite the entire array each time a new line comes in? I also
have a
feeling this might cause massive screen flashing, but at least you'll
know
it's doing something! Is there a better way?

I don't think you would experience flashing, necessarily. You may, of
course, have noticeable delays depending on how much text you're dealing
with, but assuming the control doesn't get cleared visually during the
time it takes to copy the data (and I don't know why it would), you won't
have any worse flashing than usual.

If you are going to set your limit based on when you run out of memory,
then I would guess that the mere act of setting the new text for the
control may be very slow with that much text. You may find that a limit
that is *considerably* smaller than what is theoretically possible is
better for performance.

Now, that in mind, I think your best bet is to maintain the list of
strings for your control as a generic LinkedList<>. That way, the only
real performance hit (not counting the memory consumption) is in setting
the text of the control itself. If you are actually trying to remove the
first element from an array every time you want to trim your text, then
the entire array needs to be copied each time that happens. You can
either do this in situ with your own loop, or you can create a new array
and use the Array.Copy() method to copy the data over (the latter having
the additional problem of requiring yet another copy of the array in
memory). But either way, you basically have to copy each element.

On the bright side, you're only copying string references, rather than the
strings themselves, but it sounds like we're talking about a pretty big
array, so even so you're likely to run into performance issues if you try
to maintain everything in an array. If you use a LinkedList<> then at
least the element removal and additions are fast, leaving you only the
overhead of copying from the LinkedList<> instance to an array for the
purpose of changing the text in the control. This would roughly double
performance as compared to maintaining the data as an array that you have
to copy each time.

If you really want to support a huge amount of text and you want
performance to be good, you may find it is best to simply implement your
own text display control. Then you can use a LinkedList<> as the
underlying data storage, and include your own methods for deleting the
first line when necessary. You can even just make the control be an
automatically line-limited text buffer, including a method to add a line
of text that automatically discards the oldest line.

Oh, I'm sorry. Did you want your actual question answered? :) I'm not
aware of any built-in method to remove the first element of an Array.
However, if you use the List<> generic class, it does have a RemoveAt()
method that you can use (but it will have the same performance issues that
doing it yourself on an Array would have).

Pete
 
P

-pb-

Try using RemovAt() function of array.

But you got to remember the user may to scroll from top ot botton. If
user want to see the moved line how will you show it if its been
deleted
 
P

Peter Duniho

Try using RemovAt() function of array.

ArrayList has a RemoveAt() method. Array exposes an IList, which has
RemoveAt(), but because of the implementation of Array, the IList you get
from an array throws a NotSupportedException if you try to call it. Since
ArrayList has very much the same overhead required by the List<> generic
class (in this case, the need to call ToArray() before setting the
But you got to remember the user may to scroll from top ot botton. If
user want to see the moved line how will you show it if its been
deleted

Presumably, this is for use in some kind of scrollable text output buffer,
where it is common practice to have the oldest lines disappear off the
top. One hopes that deleting lines of text won't be a problem, given the
nature of the question. :)

Pete
 

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