我有一个文本框,可以返回各种字符串,长度从5个字符到1000个字符不等.
它具有以下属性:
它具有以下属性:
> multiline = true
> wordwrap = true
我需要设置哪些文本框的其他属性才能使以下内容成为可能?
>盒子的宽度应该是固定的
>根据文本返回的大小自动调整框的高度,例如,如果文本运行到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);
this.textBox1.Height = (textBox1.Font.Height + 2) * numberOfLines;
}
}