最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中的TextBox或RichTextBox自动滚动到最底端末尾

C# crifan 19394浏览

【问题】

C#中一个RichTextBox,当新写入内容时,超过当前显示区域后,没法自动滚动到最低端,不方便查看内容。

【解决过程】

1.参考:

Rich Text box scroll to the bottom when new data is written to it

去添加对应的事件:

        private void rtbLog_TextChanged(object sender, EventArgs e)
        {
            rtbLog.SelectionStart = rtbLog.Text.Length; //Set the current caret position at the end
            rtbLog.ScrollToCaret(); //Now scroll it automatically
        }

效果就是所希望的了,当内容变化后,自动滚动到最底端:

now can auto scroll to end

 

【总结】

主要还是利用TextBox或RichTextBox的TextChanged事件,去实现需要的效果:

        private void rtbLog_TextChanged(object sender, EventArgs e)
        {
            rtbLog.SelectionStart = rtbLog.Text.Length; //Set the current caret position at the end
            rtbLog.ScrollToCaret(); //Now scroll it automatically
        }

转载请注明:在路上 » 【已解决】C#中的TextBox或RichTextBox自动滚动到最底端末尾

81 queries in 0.153 seconds, using 19.21MB memory