C#中,如何让richTextBox旁边的滚动条始终处于最下方
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中,如何让richTextBox旁边的滚动条始终处于最下方相关的知识,希望对你有一定的参考价值。
先有两个richTextBox,当我在下面打字,就会追加到上面。我现在想怎样跟QQ的功能一样,使滚动条自动往下滚。因为每当我打新句子,虽然可以出现在上面,但是滚动条没动啊!还要拖才能看得见。。。
参考技术A 是richTextBox1的ScrollToCaret属性 ,将控件的内容滚动到当前插入符号位置,在richTextBox1_TextChanged事件中加入代码:richTextBox1.ScrollToCaret();就ok了。 参考技术B richTextBox1.ScrollToCaret();本回答被提问者采纳禁用 RichTextBox 中的滚动(c#)
【中文标题】禁用 RichTextBox 中的滚动(c#)【英文标题】:disable scrolling in RichTextBox (c#) 【发布时间】:2012-08-07 11:06:24 【问题描述】:我编写了代码以在单独的页面中显示文本,例如 Microsoft Word,我使用文本框集合,当用户填写一个文本框时,会自动显示新框,并且光标会移动到她身上。
问题是当用户在文本框中写入最后一行时,该框会向下滚动一点,正如您将在何时运行此代码时看到的那样,我该如何禁用滚动。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
public partial class Form1 : Form
List<myRTB> pages; // collection of our RichTextBox, use as pages
public Form1()
InitializeComponent();
pages = new List<myRTB>();
pages.Add(new myRTB());
pages[0].Width = 200;
pages[0].Height = 290;
pages[0].Location = new Point(50, 10);
pages[0].Name = "0";
this.Controls.Add(pages[0]);
this.Width = 300;
this.Height = 360;
this.AutoScroll = true;
public void AddPage(int correntPageIndex)
if (correntPageIndex == (pages.Count - 1))
// create a new page
pages.Add(new myRTB());
pages[correntPageIndex + 1].Width = 200;
pages[correntPageIndex + 1].Height = 290;
pages[correntPageIndex + 1].Location = new Point(50, pages[correntPageIndex].Location.Y + 300);
this.Controls.Add(pages[pages.Count - 1]);
this.Name = (correntPageIndex + 1).ToString();
bool CursorInEnd = (pages[correntPageIndex].SelectionStart == pages[correntPageIndex].TextLength);
// Transfer the last word on the previous page, to the new page
int lastLineIndex = pages[correntPageIndex].GetLineFromCharIndex(pages[correntPageIndex].TextLength - 2);
// find the index of the first char in the last line
int indexOfFirstCharInLastLine = pages[correntPageIndex].GetFirstCharIndexFromLine(lastLineIndex);
// find the index of the last space in the last line
int indexOfLastSpace = pages[correntPageIndex].Text.LastIndexOf(' ', indexOfFirstCharInLastLine);
string restOfString;
if (indexOfLastSpace < 0) // no spaces in the last line
restOfString = pages[correntPageIndex].Text.Substring(pages[correntPageIndex].TextLength - 1);
pages[correntPageIndex + 1].Text.Insert(0, restOfString);
pages[correntPageIndex].Text.Remove(pages[correntPageIndex].TextLength - 1);
else // there is spaces in the last line
restOfString = pages[correntPageIndex].Text.Substring(indexOfLastSpace + 1);
pages[correntPageIndex + 1].Text = pages[correntPageIndex + 1].Text.Insert(0, restOfString);
pages[correntPageIndex].Text = pages[correntPageIndex].Text.Remove(indexOfLastSpace + 1);
if (CursorInEnd)
// Move the cursor to next page
pages[correntPageIndex + 1].SelectionStart = restOfString.Length;
pages[correntPageIndex + 1].Focus();
class myRTB : RichTextBox
public myRTB()
this.ScrollBars = RichTextBoxScrollBars.None;
protected override void WndProc(ref Message m)
// catch the request resize message
if (m.Msg == (WM_REFLECT | WM_NOTIFY))
REQRESIZE rrs = (REQRESIZE)(Marshal.PtrToStructure(m.LParam, typeof(REQRESIZE)));
if (rrs.nmhdr.code == EN_REQUESTRESIZE)
if (rrs.rc.ToRectangle().Height > this.ClientRectangle.Height)
((Form1)Parent).AddPage(int.Parse(this.Name));
base.WndProc(ref m);
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
public IntPtr HWND;
public uint idFrom;
public int code;
public override String ToString()
return String.Format("Hwnd: 0, ControlID: 1, Code: 2",
HWND, idFrom, code);
[StructLayout(LayoutKind.Sequential)]
public struct REQRESIZE
public NMHDR nmhdr;
public RECT rc;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
public int Left, Top, Right, Bottom;
public override string ToString()
return String.Format("0, 1, 2, 3", Left, Top, Right,
Bottom);
public Rectangle ToRectangle()
return Rectangle.FromLTRB(Left, Top, Right, Bottom);
public const int WM_USER = 0x400;
public const int WM_NOTIFY = 0x4E;
public const int WM_REFLECT = WM_USER + 0x1C00;
public const int EN_REQUESTRESIZE = 0x701;
【问题讨论】:
【参考方案1】:为确保文本不会自动滚动,请查看以下类似问题的答案。
Disabling RichTextBox autoscroll
这是您问题的另一个很好的答案:
Prevent Autoscrolling in RichTextBox
我从上面的链接中复制了代码,请确保为提供此代码的用户提供信用(它不是我的)
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
const int WM_USER = 0x400;
const int EM_HIDESELECTION = WM_USER + 63;
void OnAppend(string text)
bool focused = richTextBox1.Focused;
//backup initial selection
int selection = richTextBox1.SelectionStart;
int length = richTextBox1.SelectionLength;
//allow autoscroll if selection is at end of text
bool autoscroll = (selection==richTextBox1.Text.Length);
if (!autoscroll)
//shift focus from RichTextBox to some other control
if (focused)
textBox1.Focus();
//hide selection
SendMessage(richTextBox1.Handle, EM_HIDESELECTION, 1, 0);
richTextBox1.AppendText(text);
if (!autoscroll)
//restore initial selection
richTextBox1.SelectionStart = selection;
richTextBox1.SelectionLength = length;
//unhide selection
SendMessage(richTextBox1.Handle, EM_HIDESELECTION, 0, 0);
//restore focus to RichTextBox
if(focused) richTextBox1.Focus();
【讨论】:
在我的示例中,我已经编写了 this.ScrollBars = RichTextBoxScrollBars.None; 它使得滚动条不显示,但文本框仍在滚动。 抱歉,错过了非常重要的代码行 :) 我更新了我的答案,我想它会对你有所帮助。 但是,在你输入之后,如果你将光标移动到第一页的最后一行,文本框会向下滚动一点,并在该行下方显示一个空格。 嗯,也许可以尝试在焦点事件下添加一些代码来检查该页面末尾是否有任何新行,如果是,则删除该行,禁用滚动并获得下一个richtexboxt的焦点?这是一个肮脏的工作,但它可能只是工作。 Focus() 其他一些控件似乎是这里的关键。以上是关于C#中,如何让richTextBox旁边的滚动条始终处于最下方的主要内容,如果未能解决你的问题,请参考以下文章