Prevent RichTextBox from auto scrolling

♀尐吖头ヾ 提交于 2020-01-05 12:13:49

问题


I want to append text to a RichTextBox control but I don't want to interfere with the user's scroll position or text selection. The best that I've been able to do so far is to save the SelectionStart and SelectionLength properties and restore them after I append my text. This is close but the cursor ends up at the top of the control rather than wherever it was prior to the append. Is this possible?


回答1:


Ok here is exactly what you need: Richtextbox :- controlling scrolling when appending text.

There you will find how to lock the scroll when appending text.

Look for CoolColin answer:

The trick is to use the Windows EM_HIDESELECTION call (this is different to the # hideselection), and also to temporarily remove the focus.

You can use the SelectionStart property of your RichTextBox control to "Gets or sets the starting point of text selected in the text box." with the values you already stored.

Have a look at RichTextBox Class. At the end you need to use TextBoxBase.ScrollToCaret method.



来源:https://stackoverflow.com/questions/6695607/prevent-richtextbox-from-auto-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!