自动调整文本框控件垂直

编程入门 行业动态 更新时间:2024-10-26 02:36:07
本文介绍了自动调整文本框控件垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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.

更多推荐

自动调整文本框控件垂直

本文发布于:2023-11-11 23:47:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   文本框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!