日期:2014-05-18 浏览次数:21387 次
private void textBox5_TextChanged(object sender, System.EventArgs e)
{
TextBox tb=(TextBox) sender;
ShowScrollBar(tb);
}
private void ShowScrollBar(TextBox tb)
{
//TextBox 当超出界面时显示垂直滚动条
if(tb.Lines.Length>tb.Height/tb.Font.Height)
{
tb.ScrollBars=System.Windows.Forms.ScrollBars.Vertical;
}
else
{
tb.ScrollBars=System.Windows.Forms.ScrollBars.None;
}
}
------解决方案--------------------
private void textBox1_TextChanged(object sender, EventArgs e)
{
int t = getline();
Graphics g = this.textBox1.CreateGraphics();
SizeF ss = g.MeasureString("测试", this.textBox1.Font);
int h = (int)ss.Height + 3;
if (t <= 5)
{
this.textBox1.Height = t * h;
this.textBox1.ScrollBars = ScrollBars.None;
}
else
{
this.textBox1.Height = 5 * h;
this.textBox1.ScrollBars = ScrollBars.Vertical;
}
}
private int getline()
{
Graphics g = this.textBox1.CreateGraphics();
int lines=0;
foreach(string s in this.textBox1.Lines)
{
SizeF ss=g.MeasureString(s,this.textBox1.Font);
lines += ((int)ss.Width / this.textBox1.Width + 1);
}
return lines;
}