简体   繁体   中英

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

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!

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.

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.

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.

如果您可以获得富文本框的滚动控件,您应该能够获取其SmallChange属性并使用它来滚动文本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM