我有一个文本框,可以返回长度从 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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)