Algirdyz
Algirdyz

Reputation: 617

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. How would I do this?

I am using AppendText and ScrollToEnd methods to update my textbox.

Thanks.

Code: nothing really special here.

xaml -

<TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="12,6,6,12" x:Name="Output" IsReadOnly="True" Grid.Row="1" />

xaml.cs

private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
    Output.AppendText(string.Format("{0} --- {1}", DateTime.Now, e.UserState));
    Output.ScrollToEnd();
}

Upvotes: 0

Views: 791

Answers (1)

yash
yash

Reputation: 812

This code is written for Winform Application but you can use it in wpf applicaton too:-

             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

Upvotes: 1

Related Questions