Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This should be easy but I can't find the solution.

I have a readonly multiline-textbox (textarea) that I am using as STDOUT.
Whenever the text data gets too long, I want to delete the oldest lines , thus text won't grow too big.

I add lines with:
textbox01.appendText("hello world\n");

and methods like:
int linecount = textbox01.Lines.count();<br />
string tempstring = textbox01.lines[5];


I want to check if the linecount is greater than 1000, if so, then delete the first (that would be the oldest) 300 lines.
This way the "stdout file" is never more than 1000 lines and it always has the last(recent) lines written.

TIA, Thanks in advance,
Jleslie48
Posted
Updated 30-Jul-10 5:18am
v3

Don't use a TextBox.
It is very inefficient for what you want to do - every time to add a line, you generate a whole new string the size of the new textbox content, and copy the one one in!

Use a ListView instead. Set
listView1.View = View.Details;
Then use:
listView1.Items.Add(DateTime.Now.ToString());
while (listView1.Items.Count > 20)
    {
    listView1.Items.RemoveAt(0);
    }
 
Share this answer
 
I don't think you understand what the difference really is between a TextBox and a ListBox. Personally, I can see why you wouldn't want a ListBox, but memory management is in no way the same. If you want to see how it's really done, I suggest getting the .NET reflector and looking at it.

A TextBox doesn't store the text as an array of strings representing the lines. When you call .Lines[xxx], the TextBox first builds the array of strings representing each line. Then, it passes the whole array and you select the one you want.

Though AppendText is similar to the ListBox.Items.Add. However, the line deletion will be less efficient.

Anyway, how about this:

C#
output02.Text = output02.Text.Remove(0,output02.GetFirstCharIndexFromLine(5));


Also, just some hints with scrolling and flickering. If you want to pause the redrawing of the textBox so that the user can't see the scroll changing, you can use the code found at: Preventing controls from redrawing[^]

It's in VB, but you should be able to convert it easily. It will make it look better.
 
Share this answer
 
Comments
jleslie48 30-Jul-10 15:16pm    
excellent! this works perfectly! thanks!

continuing my education,

my issues with listbox are

1) the inability to select all and cut and paste all the selections into say a word document.
- now I know I had to jump through quite a bit of hoops to get cut and paste to work with textboxes, perhaps they will work on listboxes as well.

2) I still cannot see why a listbox would be better. I full agree that I don't know what the difference is between a listbox and a textbox; I'm brand new to C# and to visual studio in general. all these methods/members/properties are a bit overwhelming. Coming from unix and C, this is a mountain of stuff to know. I mean how was I going to find "getfirstcharindexfromline" member?
(BTW, I actually tried &output02.lines[5][0] to accomplish the same thing.)
Toli Cuturicu 31-Jul-10 8:21am    
"I mean how was I going to find "getfirstcharindexfromline" member?"
1. Using Intellisense
2. Reading documentation
3. Googling
4. Asking us (what you have done first)
jleslie48 2-Aug-10 7:28am    
Reason for my vote of 4
works and is simple.
listview has problems with the user. he can't select all, cut and paste into another program.

remember, I want to use this as stdout, and as such, I want the user to be able to select, cut and paste the output into his/her reports/spreadsheets/documents.


this solution works:
C#
output02.AppendText(textBox1.Text +"\r\n");

//tempstring = output02.Lines[4];
//tempint = output02.Lines.Count();
//tempint = output02.Lines[3].Length;

if (output02.Lines.Count() > 10) { //then
     MessageBox.Show("textbox is over 10 removing first 5 line");
     for (int icount = 0; icount < 5; icount++) {
                      output02.Text = output02.Text.Remove(0,output02.Lines[0].Length+2); //add 2 for the \r\n in the append text.
                      }//for
              }//then


   output02.SelectionStart = output02.Text.Length;
   output02.ScrollToCaret();

but I don't think its optimal.

ps:
"Don't use a TextBox.It is very inefficient for what you want to do - every time to add a line,, you generate a whole new string the size of the new textbox content, and copy the one one in!"


I don't think that is true if you use the .Lines[xxx] method of textboxes.
Listboxes are just a formated textbox, memory management is the same.

textbox.appendtext should be exactly the same as listbox.items.add
 
Share this answer
 
v4
Comments
Toli Cuturicu 31-Jul-10 8:18am    
Reason for my vote of 1
fake answer, anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900