我的程序调用Java,然后将标准输出重定向到一个RichTextBox
。我的问题是,每次写入数据时,垂直滚动条总是位于框的顶部。
即使您滚动到底部,一旦写入了新数据,它也会转到顶部。我想要相反的。
因此,当写入新数据时,它会留在底部。我该怎么做呢?
发布于 2012-02-24 02:11:31
可以,您可以使用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();
}
发布于 2014-02-01 05:39:48
如果RichTextBox具有焦点,并且您使用AppendText添加信息,则它将一直滚动到末尾。如果您将HideSelection设置为False,它将在失去焦点时保留其选择,并保持自动滚动。
我设计了一个使用以下方法的日志查看器GUI。它使用了一个完整的核心来跟上。删除此代码并将HideSelection设置为False后,CPU使用率降至1-2%。
//Don't use this!
richTextBox.AppendText(text);
richTextBox.ScrollToEnd();
发布于 2018-05-08 23:04:32
[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);
通过使用上述方法,您可以将富文本框滚动到底部
https://stackoverflow.com/questions/9416608
复制相似问题