Inconsistent Results with RichTextBox ScrollToCare

2019-01-27 22:25发布

I am working with a RichTextBox in C#. It exists on a TabPage. When the TabPage is selected, I aim to populate the RichTextBox, and scroll to the end. I have tried the slight variations on solutions for this common question, the main one being along the lines of:

MyRichTextBox.Select(MyRichTextBox.Text.Length, 0);  
MyRichTextBox.ScrollToCaret();  

or:

MyRichTextBox.SelectionStart = MyRichTextBox.Text.Length;  
MyRichTextBox.ScrollToCaret();  

This is producing inconsistent results, albeit in a predictable manner. It will alternate between scrolling to the bottom, and scrolling one line short of the bottom. Respectively illustrated (sorry for the links, new user so I can't post the images):
Successfully scrolled to bottom
Scrolled to one line short of the bottom
I am surprised to find nothing mentioning this behaviour through my searches, and have decided to ask if anyone here has encountered this, and/or has a solution in mind. If it comes down to it, I suppose I can go with something along the lines of itsmatt's answer.

3条回答
做个烂人
2楼-- · 2019-01-27 22:59

I did some further experimentation with ScrollToCaret and it just does not end up in the same position every time. Since my goal is limited to only scrolling all the way to the bottom, it was then a good candidate for sending the WM_VSCROLL message (277, or 0x115) to the control, with wParam of SB_PAGEBOTTOM (7). This consistently scrolls all the way to the very bottom exactly like I needed:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private const int WM_VSCROLL = 277;
private const int SB_PAGEBOTTOM = 7;

public static void ScrollToBottom(RichTextBox MyRichTextBox)
{
    SendMessage(MyRichTextBox.Handle, WM_VSCROLL, (IntPtr)SB_PAGEBOTTOM, IntPtr.Zero);
}
查看更多
萌系小妹纸
3楼-- · 2019-01-27 23:15

Change this to fit your working code..

String gotoCaret = "Something on this line.";
int position = textBox.Text.IndexOf(gotoCaret);
MyRichTextBox.SelectionStart = position;
MyRichTextBox.ScrollToCaret();
查看更多
beautiful°
4楼-- · 2019-01-27 23:16

I have the same problem, I guess a RTB is almost quite managed by Windows Messages so it sounds a bit like a rabbit warren. I don t know, therefore, the reason for the alternating output (but it has slightly a bug taste). I am concerned with this RTB.Scrolltocaret flickering output but in a VB program. Compliments for your drastic solution: It works perfectly.

Should anyone encounter this anomaly in that programming environment, here's the VB code

Imports System.Runtime.InteropServices
Public Class Form
<DllImport("user32.dll",CharSet:=CharSet.Auto)> _
Public Shared Function SendMessage( _
ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer
End Function
Const WM_SCROLL = 277
Const SB_PAGEBOTTOM = 7
Sub ScrollToBottom(ByVal RTBName As RichTextBox)
   SendMessage(RTBName.Handle, _
               WM_SCROLL, _
               SB_PAGEBOTTOM, _
               IntPtr.Zero)
End Sub 'then call ScrollToBottom instead of ScrollToCaret
查看更多
登录 后发表回答