C# 当新数据写入时,富文本框滚动到底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9416608/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:18:58  来源:igfitidea点击:

Rich Text box scroll to the bottom when new data is written to it

c#winformsrichtextbox

提问by user1158745

My program calls Java and then redirects stdout to a RichTextBox. My problem is that the vertical scrollbar always stays at the top of the box every time data is written.

我的程序调用 Java,然后将标准输出重定向到RichTextBox. 我的问题是每次写入数据时垂直滚动条总是停留在框的顶部。

Even if you scroll to the bottom, once new data has been written it goes to the top. I would like the opposite.

即使您滚动到底部,一旦写入新数据,它就会转到顶部。我想相反。

So when new data is written, it stays at the bottom. How can I do this?

所以当写入新数据时,它会留在底部。我怎样才能做到这一点?

采纳答案by Omar

Yes, you can use the ScrollToCaret()method:

是的,您可以使用以下ScrollToCaret()方法:

// bind this method to its TextChanged event handler:
// richTextBox.TextChanged += richTextBox_TextChanged;
private void richTextBox_TextChanged(object sender, EventArgs e) {
   // set the current caret position to the end
   richTextBox.SelectionStart = richTextBox.Text.Length;
   // scroll it automatically
   richTextBox.ScrollToCaret();
}

回答by Gayan Dasanayake

When writing new data, if you use AppendText()it wont scroll up and will always stay at the bottom.

写入新数据时,如果使用AppendText()它不会向上滚动并始终停留在底部。

回答by DrWu

The RichTextBox will stay scrolled to the end if it has focus and you use AppendText to add the information. If you set HideSelection to False it will keep its selection when it loses focus and stay auto scrolled.

如果 RichTextBox 具有焦点并且您使用 AppendText 添加信息,它会一直滚动到最后。如果您将 HideSelection 设置为 False,它将在失去焦点时保持其选择并保持自动滚动。

I designed a Log Viewer GUI that used the method below. It used up to a full core keeping up. Getting rid of this code and setting HideSelection to False got the CPU usage down to 1-2%

我设计了一个使用以下方法的日志查看器 GUI。它使用了一个完整的核心跟上。删除此代码并将 HideSelection 设置为 False 使 CPU 使用率降低到 1-2%

//Don't use this!
richTextBox.AppendText(text);  
richTextBox.ScrollToEnd();

回答by DocWho

This is an old question, but I had this problem and I used the richTextBox_TextChangedevent as above, which works. But I feel this is a workaround and wanted to document the actual solution in case anybody else looks for it.

这是一个老问题,但我遇到了这个问题,我使用了richTextBox_TextChanged上面的事件,它有效。但我觉得这是一种解决方法,并希望记录实际解决方案,以防其他人寻找它。

If you append it will auto-scroll, however the RichTextBoxhas to be focused. So call Focusbefore AppendTextto make sure it auto-scrolls.

如果你附加它会自动滚动,但是RichTextBox必须聚焦。所以调用Focus之前AppendText,以确保它自动滚动。

richTextBox.Focus();

richTextBox.AppendText(text);

回答by Prem Kumar Badri

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

    internal static void ScrollToBottom(RichTextBox richTextBox)
    {
        SendMessage(richTextBox.Handle, WM_VSCROLL, (IntPtr)SB_PAGEBOTTOM, IntPtr.Zero);
        richTextBox.SelectionStart = richTextBox.Text.Length;
    }

ScrollToBottom(richTextBox);

ScrollToBottom(richTextBox);

by using above method you can scroll rich text box to bottom

通过使用上述方法,您可以将富文本框滚动到底部

回答by 123iamking

I'll keep it simple:

我会保持简单:

  • Set HideSelectionproperty to false

  • Use AppendText()method to add text to RichTextBox.

  • HideSelection属性设置为false

  • 使用AppendText()方法向 RichTextBox 添加文本。

Code:

代码:

RichTextBox rtbTest;

void InitRichTextBox()
{
    //Init rtbTest...

    rtbTest.HideSelection = false;//Hide selection so that AppendText will auto scroll to the end
}

void AddText(string txt)
{
    rtbTest.AppendText(txt);
}