在 C# 表单中,我有一个面板锚定所有侧面,内部有一个文本框,锚定顶部/左侧/右侧。
当文本加载到文本框中时,我希望它自动垂直扩展,这样我就不需要滚动文本框(如果有更多文本不适合面板,最多滚动面板)。
有什么方法可以用文本框做到这一点吗? (我不限于使用此控件,因此如果有其他符合描述的控件,请随时提及)
当前选择的答案不处理没有空格的行,例如“jjjjjjjjjjjjjjjjjjjj”x1000(想想如果有人粘贴 URL 会发生什么)
这段代码解决了这个问题:
private void txtBody_TextChanged(object sender, EventArgs e)
{
// amount of padding to add
const int padding = 3;
// get number of lines (first line is 0, so add 1)
int numLines = this.txtBody.GetLineFromCharIndex(this.txtBody.TextLength) + 1;
// get border thickness
int border = this.txtBody.Height - this.txtBody.ClientSize.Height;
// set height (height of one line * number of lines + spacing)
this.txtBody.Height = this.txtBody.Font.Height * numLines + padding + border;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)