使用最新数据限制文本区域 [英] Limit textarea with latest data

查看:81
本文介绍了使用最新数据限制文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易,但是我找不到解决方案.

我有一个只读的多行文本框(textarea),正在用作STDOUT.
每当文本数据过长时,我都想删除最旧的行,这样文本就不会变得太大.

我添加以下行:
textbox01.appendText("hello world\n");

和类似的方法:

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];



我想检查行数是否大于1000,如果是,则删除第一行(这将是最旧的)300行.
这样,"stdout文件"永远不会超过1000行,并且始终会写入最后(最近)的行.

TIA,在此先感谢,
Jleslie48



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

推荐答案

不要使用TextBox.
这对于您想做的事情来说效率很低-每次添加一行,都会生成一个新字符串,其大小与新文本框内容的大小相同,然后将其中一个复制进来!

请改用ListView.设置
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;

然后使用:

listView1.Items.Add(DateTime.Now.ToString());
while (listView1.Items.Count > 20)
    {
    listView1.Items.RemoveAt(0);
    }


我不认为您了解TextBox和ListBox的真正区别是什么.就个人而言,我明白了为什么您不想要ListBox了,但是内存管理却完全不同.如果您想了解它是如何完成的,我建议您安装.NET反射器并对其进行观察.

TextBox不会将文本存储为表示行的字符串数组.当您调用.Lines [xxx]时,TextBox首先构建代表每行的字符串数组.然后,它传递整个数组,然后选择所需的数组.

尽管AppendText与ListBox.Items.Add类似.但是,删除行的效率会降低.

无论如何,这是怎么回事:

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:

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



另外,只有一些滚动和闪烁的提示.如果要暂停重绘textBox以便用户看不到滚动变化,则可以使用位于以下位置的代码:



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.


listview用户遇到问题.他不能全部选择,不能剪切并粘贴到另一个程序中.

记住,我想将此用作标准输出,因此,我希望用户能够选择,剪切输出并将其粘贴到他/她的报告/电子表格/文档中.


此解决方案有效:
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:
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();


但我认为它不是最佳的.

ps:
不要使用TextBox."对于您要执行的操作效率很低-每次添加一行时,您都会生成一个新字符串,其大小与新textbox内容的大小相同,然后复制一个进来!"


如果您使用文本框的.Lines [xxx]方法,我认为这是不正确的.
列表框只是一个格式化的文本框,内存管理是相同的.

textbox.appendtext应该与listbox.items.add


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


这篇关于使用最新数据限制文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆