C#调整文本框大小以适应内容

我正在编写一个程序,用户应该能够在文本框中编写文本。 我希望文本框能够自行resize,因此它适合内容。 我尝试过以下方法:

private void textBoxTitle_TextChanged(object sender, TextChangedEventArgs e) { System.Drawing.Font myFont = new System.Drawing.Font("Verdana", 8); System.Drawing.SizeF mySize = e.Graphics.MeasureString("This is a test", myFont); this.textBoxTitle.Width = (int)Math.Round(mySize.Width, 0); } 

我收到一个错误,说Graphics不适用于TextChangedEventArgs。 还有其他方法可以调整文本框的大小吗?

您应该尝试类似下面的代码。 它对我有用。

 private void textBox1_TextChanged(object sender, EventArgs e) { Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font); textBox1.Width = size.Width; textBox1.Height = size.Height; } 

有关更多信息,请参阅TextRenderer.MeasureText()

我正在添加这个答案,因为我没有看到任何其他文本框中的fixed width方面。 如果文本框的宽度固定,并且只想调整其高度,则可以执行以下操作:

像这样的东西给出了文本的高度,就像在多行自动换行文本框中绘制它一样:

 SizeF MessageSize = MyTextBoxControl.CreateGraphics() .MeasureString(MyTextBoxControl.Text, MyTextBoxControl.Font, MyTextBoxControl.Width, new StringFormat(0)); 

我不确定StringFormat应该是什么,但StringFormatFlags值似乎不适用于默认的TextBox组合。

现在使用MessageSize.Height您可以知道文本框中文本的高度。

您绑定到错误的事件,并且您不能使用TextChangedEventArgs对象中的图形对象。

尝试使用TextChanged事件。 以下代码段正在运行:

 public Form1() { InitializeComponent(); this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged); } void textBox1_TextChanged(object sender, EventArgs e) { System.Drawing.SizeF mySize = new System.Drawing.SizeF(); // Use the textbox font System.Drawing.Font myFont = textBox1.Font; using (Graphics g = this.CreateGraphics()) { // Get the size given the string and the font mySize = g.MeasureString(textBox1.Text, myFont); } // Resize the textbox this.textBox1.Width = (int)Math.Round(mySize.Width, 0); } 

}

我有同样的问题,我以更简单的方式解决了它。

我使用了Label控件的AutoSize属性。我在表单中添加了一个不可见的标签,将其AutoSize属性设置为True。 当我需要更改TextBox的大小时,我使用以下代码:

 MyLabel.Text = MyTextBox.Text; MyTextBox.Size = MyLabel.Size; 

我设置了标签的最大和最小尺寸以获得更好的效果。 玩得开心

您将需要使用表单的CreateGraphics()方法来创建Graphics实例来测量字符串。

TextChangedEventArgs类没有Graphics属性,这是传递给Paint事件处理程序的PaintEventArgs类的属性。

试试这个:

 using System.Drawing; ... private void textBoxTitle_TextChanged(object sender, TextChangedEventArgs e) { // Determine the correct size for the text box based on its text length // get the current text box safely TextBox tb = sender as TextBox; if (tb == null) return; SizeF stringSize; // create a graphics object for this form using(Graphics gfx = this.CreateGraphics()) { // Get the size given the string and the font stringSize = gfx.MeasureString(tb.Text, tb.Font); } // Resize the textbox tb.Width = (int)Math.Round(stringSize.Width, 0); } 

基本上,您为表单创建自己的Graphics对象,然后根据TextBox的文本和字体进行测量。 using将正确处理Graphics对象 – 您之前的代码可能会泄漏!

您是否尝试设置yourTextBox.AutoSize = true; ? 此属性可能隐藏在GUI设计器中,但您可以在InitializeComponent();之后立即在表单构造函数中设置它InitializeComponent(); 呼叫。

你可以做的Graphics.Measure字符串是PaintEventArgs ,而不是TextChangedEventArgs

我认为你想要的是这个

 System.Drawing.Font myFont = new System.Drawing.Font("Verdana", 8); Graphics graphics = this.CreateGraphics(); SizeF textSize = graphics.MeasureString("This is a test", myFont); 

问题是你不能通过简单地分配它来创建一个Graphics对象,因为它没有公共构造函数,所以你最好去使用TextRenderer.MeasureText,就像在http://msdn.microsoft.com/en-us/中所做的那样。 库/ y4xdbe66.aspx

TextRenderer不太准确,因为它使用GDI而Graphics使用GDI +,所以也许你应该在Width属性的值上留一点余量。

希望这可以帮助

无论目标是什么。

如果文本框的大小应该根据字符串动态设置,该字符串应该是此框内的文本, 没有不错的选项

理由: MeasureString使用通常的字符串格式化程序作为其自身宽度和高度的分隔符。 手段,回车和换行也被解析。 导致sizeF.Width和sizeF.Height。

根据字符串(及其字体和行数),这两个变量都可以携带值,这些值有时无法用作文本框的宽度/高度值(因为它们可能大于父窗体的值,这会resize文本框大小,左边框和底边框超出父窗体的边框 )。

一些解决方案仍然可用,取决于目标,一个人想要实现。

一个想法是:在设计器中创建一个文本框,大小= 100 X 100. 启用自动换行

在文本框的OnTextChanged事件处理程序中,我们只是将文本框的宽度调整为由selfelf定义的值(例如parentform.Width或其他硬值)。

这将导致自动换行重新计算文本框中的字符串 ,这将重新排列文本框中的所有字符,因为启用了自动换行。

文本框的高度可以很难设置为parentform.Height,例如。

但是,更好:根据方法texbox.GetPositionFromCharIndex(textbox.TextLength -1)的ReturnValue(Point)的Y值动态设置高度。 然后,使用Math.Min()确定,它更小(parentform.Height或Point.Y),并将文本框大小重置为新的Size(previousDeterminedWidth,nowDeterminedHeight)。

请记住(如果启用了滚动条),可以为您的宽度计算添加大约17个像素。

最好的祝福