【问题标题】:How to make a richTextBox scroll?如何使richTextBox 滚动?
【发布时间】:2012-05-18 01:49:52
【问题描述】:

我在richTextbox1 中添加了很多文本,我在右侧有一个滚动条,但里面的文本是一直添加滚动条的行。 只有当我用鼠标向下拖动右侧的栏时,我才会看到文本滚动。

一旦里面的文本到达richTextbox的底部,我想让它自动滚动。

我该怎么做?

【问题讨论】:

标签: c# winforms


【解决方案1】:

这段代码可以完美运行: ------

namespace CustomRTB
{
    public class CustomRTB : RichTextBox
    {
        #region API Stuff
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetScrollPos(IntPtr hWnd, int nBar);

        [DllImport("user32.dll")]
        private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);

        private const int SB_HORZ = 0x0;
        private const int SB_VERT = 0x1;
        #endregion
        public int HorizontalPosition
        {
            get { return GetScrollPos((IntPtr)this.Handle, SB_HORZ); }
            set { SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true); }
        }

        public int VerticalPosition
        {
            get { return GetScrollPos((IntPtr)this.Handle, SB_VERT); }
            set { SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true); }
        }
    }
}

您可以检查 RichTextBox 类它包含的属性

public RichTextBoxScrollBars ScrollBars { get;放; }

此属性使您可以向 RichTextBox 控件的用户提供水平和垂直滚动条以启用滚动。

【讨论】:

    【解决方案2】:

    每当在 RichTextBox 控件中添加文本时,您应该执行这些语句以将光标移动到 RichTextBox 中文本的末尾。假设 RichTextBox 控件的名称是richTextBox,你会有如下语句。

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

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2013-10-25
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多