How to move scroll bar up by one line? (In C# RichTextBox)

穿精又带淫゛_ 提交于 2020-01-13 12:18:00

问题


For my C# RichTextBox, I want to programmatically do the same thing as clicking the up arrow at the top of a vertical scroll bar, which moves the RichTextBox display up by one line. What is the code for this? Thanks!


回答1:


Here's what I do:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, 
                               UIntPtr wParam, IntPtr lParam);

then call:

SendMessage(myRichTextBox.Handle, (uint)0x00B6, (UIntPtr)0, (IntPtr)(-1));

Seems to work OK - you might need to tweak things a bit, though.

Hope that helps.




回答2:


For future reference the EM_LINESCROLL message is what you send to any multi-line edit control to set the scroll position. You can scroll vertically or horizontally. See MSDN for details.

You can also use the Rich Edit Selection method, where you set the character index (which you can get with EM_LINEINDEX) then call RichEdit.ScrollToCaret ie:

RichEdit.SelectionStart = SendMessage(RichEdit.Handle, EM_LINEINDEX, ScrollTo, 0);
RichEdit.ScrollToCaret();

This will scroll that line to the top of the edit control.




回答3:


window.scrollBy(0,20);

This will scroll the window. 20 is an approximate value I have used in the past that typically equals one line... but of course font size may impact how far one line really is.




回答4:


If you can get the scroll control for the rich text box, you should be able to get its SmallChange property and use that to scroll the text.



来源:https://stackoverflow.com/questions/205794/how-to-move-scroll-bar-up-by-one-line-in-c-sharp-richtextbox

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