C# RichTextBox不用滚动条

转自:http://blog.csdn.net/happy09li/article/details/7444912

第一种思路:

richTextBox1.ScrollBars = RichTextBoxScrollBars.None;
richTextBox.ContentsResized += new ContentsResizedEventHandler(richTextBox_ContentsResized);

private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
{
    richTextBox1.Height = e.NewRectangle.Height + 10;
}

第二种思路:

1.先调用以下方法:

[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

2.设置RichTextBox:            

this.richTextBox1 = new RichTextBox();           
this.richTextBox1.Text = "contentcontentcontentcontentcontentcontentcontentcontentcontent";
this.richTextBox1.Width = this.pPanel.Width-15;
this.richTextBox1.ScrollBars = RichTextBoxScrollBars.None;
this.richTextBox1.Location = new Point(0, 0 + this.lab1.Height+10);
//得到RichTextBox高度
int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
int lc = SendMessage(this.richTextBox1.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
int sf = this.richTextBox1.Font.Height * (lc + 1) + this.richTextBox1.Location.Y;
this.richTextBox1.Height = sf;
this.richTextBox1.Resize += new EventHandler(richTextBox1_Resize);
this.Controls.Add(this.richTextBox1);

3.设置RichTextBox的Resize:

void richTextBox1_Resize(object sender, EventArgs e)
{
    int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
    int lc = SendMessage(this.richTextBox1.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
    int sf = this.richTextBox1.Font.Height * (lc + 1) + this.richTextBox1.Location.Y;
    this.richTextBox1.Height = sf;
}      

你可能感兴趣的:(text)