C# 多行文本框根据文本量自动调整高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10574998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multiline textbox auto adjust it's height according to the amount of text
提问by whytheq
I have a textbox which can return various strings ranging from 5 characters upto 1000 characters in length. It has the following properties:
我有一个文本框,它可以返回长度从 5 个字符到 1000 个字符的各种字符串。它具有以下特性:
- multiline = true
- wordwrap = true
- 多行=真
- 自动换行 = 真
Which other properties of the textbox do I need to set to make the following possible?
我需要设置文本框的哪些其他属性才能实现以下功能?
- The width of the box should be fixed
- The height of the box to auto adjust depending on how much text it is returning e.g if the text runs onto 3 lines then it adjusts to 3 lines in height.
- 盒子的宽度应该是固定的
- 要自动调整的框的高度取决于它返回的文本数量,例如,如果文本运行到 3 行,则它会调整为 3 行的高度。
采纳答案by Community Driven Business
Try this following code:
试试这个下面的代码:
public partial class Form1 : Form
{
private const int EM_GETLINECOUNT = 0xba;
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
var numberOfLines = SendMessage(textBox1.Handle.ToInt32(), EM_GETLINECOUNT, 0, 0);
this.textBox1.Height = (textBox1.Font.Height + 2) * numberOfLines;
}
}
回答by George
You need to adjust the height of the text box from code. Count the number of lines (this article here can help you with a way to do just that), then set the Textbox.Heightto the value you need (number of line * 8px or so, depending on the font used inside the TextBox).
您需要从代码中调整文本框的高度。计算行数(这里的这篇文章可以帮助您做到这一点),然后将 设置为Textbox.Height您需要的值(行数 * 8px 左右,取决于 里面使用的字体TextBox)。
In the linked article solution was to override TextBox control class to be able to get the number of lines; there might be other ways to get the number of lines, but the suggested solution in the article looks quite elegant to me.
在链接的文章中,解决方案是覆盖 TextBox 控件类以获取行数;可能还有其他方法可以获得行数,但文章中建议的解决方案对我来说看起来很优雅。
回答by Harry Cutts
There doesn't seem to be any functionality to do this built in to the TextBox class, but the Font class has a Heightproperty that returns the number of pixels between baselines.
TextBox 类中似乎没有任何功能可以执行此操作,但是 Font 类有一个Height属性,可返回基线之间的像素数。
It is also possible to find out how many lines the text in the TextBox occupies, as described in thisblog post (warning: it's not exactly elegant).
还可以找出 TextBox 中的文本占用多少行,如本博客文章中所述(警告:它并不完全优雅)。
Once you've obtained this information, you should be able to make the TextChanged handler set the height of the TextBox accordingly using some simple maths.
获得此信息后,您应该能够使用一些简单的数学运算使 TextChanged 处理程序相应地设置 TextBox 的高度。
回答by vlad
private void tb_TextChanged(object sender, EventArgs e)
{
tb.Height = (tb.Text.Split('\n').Length + 2 ) * tb.Font.Height;
}
回答by Mike de Klerk
Something like this gives the height of the text as how it is drawn in the textbox itself:
像这样的东西给出了文本的高度,因为它是如何在文本框中绘制的:
SizeF MessageSize = MyTextBoxControl.CreateGraphics()
.MeasureString(MyTextBoxControl.Text,
MyTextBoxControl.Font,
MyTextBoxControl.Width,
new StringFormat(0));
I am not sure what StringFormatshould be but the values StringFormatFlagsdo not seem to apply to a default TextBoxmake up.
我不确定StringFormat应该是什么,但这些值StringFormatFlags似乎不适用于默认TextBox组成。
Now with MessageSize.Heightyou know the height of the text in the textbox.
现在MessageSize.Height您知道文本框中文本的高度。
回答by maf-soft
I can't believe there is still no really elegant way. This is what I puzzled out:
我不敢相信仍然没有真正优雅的方式。这是我不解的地方:
textBox.Height += textBox.GetPositionFromCharIndex(textBox.Text.Length - 1).Y
+ 3 + textBox.Font.Height - textBox.ClientSize.Height;
This works by determining the pixel coordinates of the last character of the text.
这通过确定文本最后一个字符的像素坐标来工作。
You can execute this after setting the contents, i.e. in OnLoadof the Formor OnTextChangedof the TextBoxcontrol. If the fixed width changes when the user resizes the form, you should also take care of that, i.e. OnResizeor OnClientSizeChanged.
您可以在设置内容,即在执行这个OnLoad的Form还是OnTextChanged在的TextBox控制。如果用户调整表单大小时固定宽度发生变化,您也应该注意这一点,即OnResize或OnClientSizeChanged。
TextBoxsupports the AutoSizeproperty. However, it is already set to trueby default, and it is not shown in the property editor or IntelliSense. It is just for font height changes and does not work when using MultiLine = true:( - this is not mentioned in the documentation.
TextBox支持AutoSize物业。但是,true默认情况下它已设置为,并且不会显示在属性编辑器或 IntelliSense 中。它仅用于字体高度更改,在使用时不起作用: MultiLine = true(-文档中未提及。
Other options might include using a different control, like RichTextBoxor Label. I didn't try yet, but it seems that a Label supports AutoSizemuch better.
其他选项可能包括使用不同的控件,例如RichTextBox或Label。我还没有尝试,但似乎标签支持 AutoSize好得多。
回答by Bharat Prasad Satyal
Add MaxHeight property on TextBox as below.
在 TextBox 上添加 MaxHeight 属性,如下所示。
<TextBox Name="txtSample" MaxHeight="1000" />
<TextBox Name="txtSample" MaxHeight="1000" />

