我的WinForm应用中有多行TextBox Multiline = True , Scrollbar = Vertical &的自动换行=真即可。我想要做的是,每当我到达文本框的底部(通过输入文本或按Enter键),我希望表单高度随着文本框高度的增加而增加(与Windows中的StickyNote应用程序完全相同) )。我不知道从哪里开始?请帮忙。
修改
以下是我的代码
private void txtNote_TextChanged(object sender, EventArgs e)
{
int currentHeight = txtNote.Height;
int newHeight = 0;
newHeight = txtNote.PreferredHeight * txtNote.Lines.Length;
if (newHeight > currentHeight)
{
//** Sriram's suggestion
//this.ClientSize = new Size(this.ClientSize.Width, txtNote.PreferredHeight * txtNote.Lines.Length);
//** Hans's suggestion
Size sz = new Size(txtNote.ClientSize.Width, int.MaxValue);
TextFormatFlags flags = TextFormatFlags.WordBreak;
int padding = 3;
int borders = txtNote.Height - txtNote.ClientSize.Height;
sz = TextRenderer.MeasureText(txtNote.Text, txtNote.Font, sz, flags);
int h = sz.Height + borders + padding;
if (txtNote.Top + h > this.ClientSize.Height - 10)
{
h = this.ClientSize.Height - 10 - txtNote.Top;
}
txtNote.Height = h;
}