调整文本框大小以适合内容

本文关键字:文本 调整 | 更新日期: 2023-09-27 18:13:42

我正在编写一个程序,用户应该能够在TextBox中编写文本。我想要TextBox调整自己的大小,所以它适合内容。我尝试了以下方法:

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。有没有别的方法可以调整TextBox的大小?

调整文本框大小以适合内容

你应该尝试下面的代码。它对我很有效。

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,你知道文本框中文本的高度。

我也遇到过同样的问题,我用一种更简单的方法解决了。

我使用了标签控件的AutoSize属性。我添加了一个不可见的标签到我的表单,设置它的AutoSize属性为True。当我需要改变文本框的大小时,我使用以下代码:

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

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

绑定到错误的事件,并且不能在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);
}

}

首先,创建方法使TextBox适合其内容。

private void AutoSizeTextBox(TextBox txt)
{
    const int x_margin = 0;
    const int y_margin = 2;
    Size size = TextRenderer.MeasureText(txt.Text, txt.Font);
    txt.ClientSize =
        new Size(size.Width + x_margin, size.Height + y_margin);
}

然后使用TextChanged事件处理程序调用AutoSizeTextBox()函数使TextBox在文本更改时适合其文本。

private void txtContents_TextChanged(object sender, EventArgs e)
{
    AutoSizeTextBox(sender as TextBox);
}

以上就是全部,更多信息:

resize-a-textbox-to-fit-it -text

您需要使用表单的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对象—您之前的代码将会泄露得非常可怕!

无论目标是。

如果文本框的大小应该根据字符串动态设置,该字符串应该是该框中的文本,没有很好的选项

原因: MeasureString使用常规的字符串格式化器作为其宽度和高度的分隔符。方法、回车和换行也会被解析。结果是sizeF。

根据字符串(以及它的字体和行数),这两个变量都可以携带值,这些值有时用作文本框的宽度/高度值是没用的(因为它们可以比parentform的值大,这将重新调整文本框的大小,左边框和下边框超过父窗体的大小)。

仍有一些解决方案可用,这取决于你想达到的目标。

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

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

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

文本框的高度可以通过parentform设置。例如,高度。

,但更好:根据方法texbox.GetPositionFromCharIndex(textbox. getpositionfromcharindex)的ReturnValue (Point)的Y值动态设置高度。TextLength -1)。然后,使用Math.Min()确定哪个更小(无论是parentform。高度或点。Y),并将文本框的大小重置为new size (preousdeterminedwidth, nowDeterminedHeight)。

请记住(如果启用滚动条)在宽度计算中添加约17像素。

问候

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

图片。测量字符串你可以做o 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/library/y4xdbe66.aspx 中完成

TextRenderer不太准确,因为它使用GDI和图形使用GDI+,所以也许你应该在你从宽度属性获得的值上留下一点空白。

希望能有所帮助