我有一个文本框,它可以返回长度从 5 个字符到 1000 个字符不等的各种字符串。 它具有以下属性:
- 多行=真
- 自动换行=真
我需要设置文本框的哪些其他属性才能实现以下功能?
- 盒子的宽度要固定
- 根据返回的文本量自动调整框的高度,例如,如果文本占 3 行,则它会调整为 3 行的高度。
最佳答案
试试下面的代码:
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;
}
}
关于c# - 多行文本框根据文本量自动调整高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10574998/