繁体   English   中英

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

[英]C# Resize textbox to fit content

我正在编写一个程序,用户应该能够在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您知道文本框中文本的高度。

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

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

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);
}

}

不管目的是什么。

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

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

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

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

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

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

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

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

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

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

此致

您将需要使用表单的 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对象 - 您之前的代码会严重泄漏!

首先,创建方法使 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);
}

这就是全部,有关更多信息:

调整文本框大小以适应其文本

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

Graphics.Measure 字符串你可以做 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/ 中所做的那样图书馆/y4xdbe66.aspx

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

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM