Disabling RichTextBox autoscroll

我与影子孤独终老i 提交于 2019-11-29 07:32:19

If your issue is with the "Vertical Scroll" scrolling down when you are adding the Log text, but you would want it to be on top all the time:

you have to add event handlers to VScroll, TextChanged events and in the event handler set the scroll to top

richTextBox1.VScroll += HandleRichTextBoxAdjustScroll;
richTextBox1.TextChanged += HandleRichTextBoxAdjustScroll;

private const UInt32 SB_TOP = 0x6;
private const UInt32 WM_VSCROLL = 0x115;

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, UInt32 Msg,
    IntPtr wParam, IntPtr lParam);

private void HandleRichTextBoxAdjustScroll(Object sender,
    EventArgs e)
{
    PostMessage(handle, WM_VSCROLL, (IntPtr)SB_TOP, IntPtr.Zero);
}

You could do the same with horizontal scroll bar too. Replace WM_VSCROLL with WM_HSCROLL and SB_TOP with SB_LEFT

private const UInt32 WM_HSCROLL = 0x0114;
private const UInt32 SB_LEFT = 0x06;

You may try tb_logs.SelectionLength = 1; along with SelectionStart. This will make 1 char selected from your Current Position.

Not Tried it...But may work

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