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

2019-05-03 11:21发布

问题:

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.