在C#的形式,我有一个面板固定四面,并在里面,一个文本框,挂靠顶部/左/右。
In a C# form, I have a panel anchored all sides, and inside, a textbox, anchored top/left/right.
当文字被装载到文本框,我希望它自动扩大自身垂直,这样我不需要滚动文本框(滚动顶多面板,如果有不适合的面板更多的文字) 。有没有办法用一个文本框来做到这一点? (我不受限的,所以如果有适合的描述另一个控制使用这种控制,可随时提吧)
When text gets loaded into the textbox, i want it to auto expand itself vertically so that I don't need to scroll the textbox (scroll the panel at most, if there is more text that doesn't fit the panel). is there any way to do this with a textbox? (i'm not constrained to use this control so if there's another control that fits the description, feel free to mention it)
推荐答案我会认为这是一个多行文本框,你会允许它垂直增长。这code效果很好:
I'll assume this is a multi-line text box and that you'll allow it to grow vertically. This code worked well:
private void textBox1_TextChanged(object sender, EventArgs e) { Size sz = new Size(textBox1.ClientSize.Width, int.MaxValue); TextFormatFlags flags = TextFormatFlags.WordBreak; int padding = 3; int borders = textBox1.Height - textBox1.ClientSize.Height; sz = TextRenderer.MeasureText(textBox1.Text, textBox1.Font, sz, flags); int h = sz.Height + borders + padding; if (textBox1.Top + h > this.ClientSize.Height - 10) { h = this.ClientSize.Height - 10 - textBox1.Top; } textBox1.Height = h; }您应该当文本框为空,如设置属性的minimumSize做一些合理的。
You ought to do something reasonable when the text box is empty, like setting the MinimumSize property.
更多推荐
自动调整文本框控件垂直
发布评论