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

后端 未结 3 1360
半阙折子戏
半阙折子戏 2021-01-07 23:25

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

3条回答
  •  礼貌的吻别
    2021-01-08 00:03

    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

        
        
            
            

    The code behind for the same is as below:

    using System.Windows;
    
    namespace CheckRichTextBox
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        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.

提交回复
热议问题