我有一个文本框的. multiline属性设置为true。每隔一段时间,我都会向它添加新的文本行。我希望文本框自动滚动到最底部的条目(最新的一个)每当添加一个新的行。我该怎么做呢?


当前回答

我使用了一个函数:

private void Log (string s) {
    TB1.AppendText(Environment.NewLine + s);
    TB1.ScrollToCaret();
}

其他回答

对于登陆这里希望看到webforms实现的其他人,您应该使用Page Request Manager的endRequest事件处理程序(https://stackoverflow.com/a/1388170/1830512)。以下是我在母版页的内容页中为我的文本框所做的,请忽略我没有为控件使用变量的事实:

var prm = Sys.WebForms.PageRequestManager.getInstance();

function EndRequestHandler() {
    if ($get('<%= ((TextBox)StatusWindow.FindControl("StatusTxtBox")).ClientID %>') != null) {
        $get('<%= ((TextBox)StatusWindow.FindControl("StatusTxtBox")).ClientID %>').scrollTop = 
        $get('<%= ((TextBox)StatusWindow.FindControl("StatusTxtBox")).ClientID %>').scrollHeight;
    }
}

prm.add_endRequest(EndRequestHandler);
textBox1.Focus()
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

对我不起作用(Windows 8.1,不管是什么原因)。 由于我仍然使用。net 2.0,我不能使用ScrollToEnd。 但这是可行的:

public class Utils
{
    [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 multi-line text box to the bottom.
    /// </summary>
    /// <param name="tb">The text box to scroll</param>
    public static void ScrollToBottom(System.Windows.Forms.TextBox tb)
    {
        if(System.Environment.OSVersion.Platform != System.PlatformID.Unix)
             SendMessage(tb.Handle, WM_VSCROLL, new System.IntPtr(SB_BOTTOM), System.IntPtr.Zero);
    }


}

VB。NET:

Public Class Utils
    <System.Runtime.InteropServices.DllImport("user32.dll", CharSet := System.Runtime.InteropServices.CharSet.Auto)> _
    Private Shared Function SendMessage(hWnd As System.IntPtr, wMsg As Integer, wParam As System.IntPtr, lParam As System.IntPtr) As Integer
    End Function

    Private Const WM_VSCROLL As Integer = &H115
    Private Const SB_BOTTOM As Integer = 7

    ''' <summary>
    ''' Scrolls the vertical scroll bar of a multi-line text box to the bottom.
    ''' </summary>
    ''' <param name="tb">The text box to scroll</param>
    Public Shared Sub ScrollToBottom(tb As System.Windows.Forms.TextBox)
        If System.Environment.OSVersion.Platform <> System.PlatformID.Unix Then
            SendMessage(tb.Handle, WM_VSCROLL, New System.IntPtr(SB_BOTTOM), System.IntPtr.Zero)
        End If
    End Sub


End Class

我发现了一个在这篇文章中没有提到的简单的区别。

如果您将所有ScrollToCarat()调用作为表单的Load()事件的一部分,那么它将不起作用。我只是将ScrollToCarat()调用添加到窗体的Activated()事件中,它工作得很好。

Edit

重要的是,只在第一次触发表单的Activated事件时(而不是在后续激活时)进行这种滚动,否则它将在每次激活表单时进行滚动,这是您可能不希望看到的情况。

因此,如果你只是在程序加载时捕获激活()事件来滚动文本,那么你可以在事件处理程序本身中取消订阅事件,从而:

Activated -= new System.EventHandler(this.Form1_Activated);

如果每次激活表单时都需要做其他事情,则可以在activate()事件第一次触发时将bool值设置为true,这样就不必在后续激活时滚动,但仍然可以做其他需要做的事情。

同样,如果你的文本框在一个不是SelectedTab的标签上,ScrollToCarat()将没有效果。所以你至少需要让它成为滚动时的选定选项卡。你可以将代码包装在YourTab.SuspendLayout();和YourTab.ResumeLayout(假);配对,如果你的形式闪烁时,你这样做。

编辑结束

希望这能有所帮助!

当文本被更改时,这将滚动到文本框的末尾,但仍然允许用户向上滚动

outbox.SelectionStart = outbox.Text.Length;
outbox.ScrollToEnd();

在Visual Studio Enterprise 2017上测试

尝试将建议的代码添加到TextChanged事件:

private void textBox1_TextChanged(object sender, EventArgs e)
{
  textBox1.SelectionStart = textBox1.Text.Length;
  textBox1.ScrollToCaret();
}