Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RichTextBox滚动至结束_C#_Scroll_Richtextbox - Fatal编程技术网

C# RichTextBox滚动至结束

C# RichTextBox滚动至结束,c#,scroll,richtextbox,C#,Scroll,Richtextbox,我有一个RichTextBox,我想在添加新文本时自动滚动到文本末尾 这是我的密码: private void outputWindowTextChanged(object sender, EventArgs e) { rtb_outputWindow.SelectionStart = rtb_outputWindow.Text.Length; rtb_outputWindow.ScrollToCaret(); } 我手动向RichTextBox添加了一组文本,如下所示: up

我有一个RichTextBox,我想在添加新文本时自动滚动到文本末尾

这是我的密码:

private void outputWindowTextChanged(object sender, EventArgs e) {
    rtb_outputWindow.SelectionStart = rtb_outputWindow.Text.Length;
    rtb_outputWindow.ScrollToCaret();
}
我手动向RichTextBox添加了一组文本,如下所示:

updateOutputWindow("Lorem ipsum dolor sit amet, ..."); //These strings are really long
updateOutputWindow("Lorem ipsum dolor sit amet, ..."); //I shortened them for this question
结果如下:

在上面的截图中,你几乎看不出边缘下实际上有更多的文本。你也可以看看右边的滚动条,看到下面还有一点空间

在上面的屏幕截图中,我使用右侧的滚动条手动向下滚动到底部;显示之前隐藏的文本

有没有办法确保RichTextBox每次都自动滚动到最末尾?

这是根据这一点改编的,并解决了最后一行有时被截断的问题

我将答案扩展为
TextBoxBase
上的扩展方法,这样它就可以同时适用于
TextBox
RichTextBox

用法:

rtb_outputWindow.ScrollToBottom();
实施:

public static class RichTextBoxUtils
{
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int SendMessage(System.IntPtr hWnd, int wMsg, System.IntPtr wParam, System.IntPtr lParam);

    private const int WM_VSCROLL = 0x115;
    private const int SB_BOTTOM = 7;

    /// <summary>
    /// Scrolls the vertical scroll bar of a text box to the bottom.
    /// </summary>
    /// <param name="tb">The text box base to scroll</param>
    public static void ScrollToBottom(this System.Windows.Forms.TextBoxBase tb)
    {
        if (System.Environment.OSVersion.Platform != System.PlatformID.Unix)
            SendMessage(tb.Handle, WM_VSCROLL, new System.IntPtr(SB_BOTTOM), System.IntPtr.Zero);
    }

}
公共静态类RichTextBoxUtils
{
[System.Runtime.InteropServices.DllImport(“user32.dll”,CharSet=System.Runtime.InteropServices.CharSet.Auto)]
私有静态外部int SendMessage(System.IntPtr hWnd、int wMsg、System.IntPtr wParam、System.IntPtr lParam);
私有常量int WM_VSCROLL=0x115;
私有const int SB_BOTTOM=7;
/// 
///将文本框的垂直滚动条滚动到底部。
/// 
///要滚动的文本框底部
公共静态无效ScrollToBottom(此System.Windows.Forms.TextBoxBase tb)
{
if(System.Environment.OSVersion.PlatformID!=System.PlatformID.Unix)
SendMessage(tb.Handle、WM_VSCROLL、new System.IntPtr(SB_底部)、System.IntPtr.Zero);
}
}

不确定这是否有效,但尝试将插入符号位置设置到文本框的末尾:
rtb\u outputWindow.CaretPosition=rtb\u outputWindow.CaretPosition.DocumentEnd请检查此@TimothyGroote RichTextBox不包含“CaretPosition”的定义。@imsome1如果您指的是该线程的已接受答案,则这正是我当前正在做的。请尝试将“设置插入符号并滚动到它”代码直接放在上次调用
updateOutputWindow