Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a RichTextBox scroll to the end when I add a new line?

I have several read only RichTextBox's that are used for logging output. Since they're read only they don't seem to automatically scroll when the text is updated. I can use the TextChanged event to force a scroll to end, but is there not simply a way to set a property or something in the XAML so that scrolling happens like normal?

like image 568
Merad Avatar asked Apr 25 '12 02:04

Merad


People also ask

How do I make a text box rich?

To create a RichTextBox control at design-time, you simply drag and drop a RichTextBox control from the Toolbox onto a Form in Visual Studio. Once a RichTextBox is added to a Form, you can move it around and resize it using the mouse and set it's properties and events.

What is RichTextBox?

The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. This topic introduces the TextBox class and provides examples of how to use it in both Extensible Application Markup Language (XAML) and C#.

What is RichTextBox WPF?

WPF RichTextBox Overview. WPF RichTextBox is the most complete rich text editor available for WPF. Load, edit, and save formatted text as HTML or RTF documents. The C1RichTextBox control provides rich formatting, automatic line wrapping, HTML and RTF import/export, table support, images, annotations, and more.


2 Answers

I solved this problem using an Interactivity trigger and a very simple action.

The action looks like this:

public class ScrollToBottomAction : TriggerAction<RichTextBox>
{
    protected override void Invoke(object parameter)
    {
        AssociatedObject.ScrollToEnd();
    }
}

Then in my XAML I have this:

<RichTextBox IsReadOnly="True" VerticalScrollBarVisibility="Auto">
     <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <interactivity:ScrollToBottomAction/>
            </i:EventTrigger>
     </i:Interaction.Triggers>
</RichTextBox>
like image 43
Samuel Jack Avatar answered Oct 25 '22 08:10

Samuel Jack


I had googled for your problem and found this post. In the section "Programming the RichTextBox" author had described about getting the behavior what you had been expecting.

Please check and let me know if it is of any use.


I tried to reproduce your problem and came up with the following solution

    <Window x:Class="CheckRichTextBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="170" Width="300">
    <StackPanel>
        <RichTextBox Height="100" Name="richTextBox1" IsReadOnly="True" VerticalScrollBarVisibility="Visible"/>
        <Button Name="btnAdd" Content="Click me to add text" VerticalAlignment="Bottom" Click="BtnAddClick" />
    </StackPanel>
</Window>

The code behind for the same is as below:

using System.Windows;

namespace CheckRichTextBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            richTextBox1.AppendText("You had Clicked the button for adding text\n");
            richTextBox1.ScrollToEnd();
        }
    }
}

This solves the problem of autoscroll, please check it and let me know if it is of any help.

like image 70
Pank Avatar answered Oct 25 '22 09:10

Pank