如何将滚动条向上移动一行?(在 C# RichTextBox 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/205794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move scroll bar up by one line? (In C# RichTextBox)
提问by JohnnyM
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!
对于我的 C# RichTextBox,我想以编程方式执行与单击垂直滚动条顶部的向上箭头相同的操作,这会将 RichTextBox 显示向上移动一行。这是什么代码?谢谢!
采纳答案by itsmatt
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.
希望有帮助。
回答by Ray Jezek
window.scrollBy(0,20);
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.
这将滚动窗口。20 是我过去使用的近似值,通常等于一行……但当然字体大小可能会影响一行的实际距离。
回答by plinth
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.
如果您可以获得富文本框的滚动控件,您应该能够获得它的 SmallChange 属性并使用它来滚动文本。
回答by itsmatt
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 MSDNfor details.
EM_LINESCROLL 消息是您发送到任何多行编辑控件以设置滚动位置的内容,以供将来参考。您可以垂直或水平滚动。有关详细信息,请参阅MSDN。
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:
您还可以使用 Rich Edit Selection 方法,在其中设置字符索引(您可以使用 EM_LINEINDEX 获得)然后调用 RichEdit.ScrollToCaret 即:
RichEdit.SelectionStart = SendMessage(RichEdit.Handle, EM_LINEINDEX, ScrollTo, 0);
RichEdit.ScrollToCaret();
This will scroll that line to the top of the edit control.
这会将该行滚动到编辑控件的顶部。