[英]Line limit for an output textbox in WPF
I have a textbox output in my application. 我的应用程序中有一个文本框输出。 I would like to add a feature that would start deleting old lines when the textbox reaches a certain limit - say 100 lines.
我想添加一个功能,当文本框达到某个限制(例如100行)时,该功能将开始删除旧行。 How would I do this?
我该怎么做?
I am using AppendText and ScrollToEnd methods to update my textbox. 我正在使用AppendText和ScrollToEnd方法来更新我的文本框。
Thanks. 谢谢。
Code: nothing really special here. 代码:这里没有什么特别的。
xaml - XAML-
<TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="12,6,6,12" x:Name="Output" IsReadOnly="True" Grid.Row="1" />
xaml.cs xaml.cs
private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
Output.AppendText(string.Format("{0} --- {1}", DateTime.Now, e.UserState));
Output.ScrollToEnd();
}
This code is written for Winform Application but you can use it in wpf applicaton too:- 这段代码是为Winform应用程序编写的,但是您也可以在wpf applicaton中使用它:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string[] lines = richTextBox1.Lines;
int x = lines.Length;
if(x>100)
{
richTextBox1.Lines = richTextBox1.Lines.Skip(x - 100).ToArray();
richTextBox1.ScrollToCaret();
richTextBox1.Select(richTextBox1.Text.Length, 0);
}
}
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.