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

2019-05-03 11:22发布

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!

4条回答
甜甜的少女心
2楼-- · 2019-05-03 11:36

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楼-- · 2019-05-03 11:39

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.

查看更多
做自己的国王
4楼-- · 2019-05-03 11:53

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.

查看更多
劳资没心,怎么记你
5楼-- · 2019-05-03 11:54

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.

查看更多
登录 后发表回答