English 中文(简体)
C# 调整案文箱以适应内容
原标题:C# Resize textbox to fit content

I m writing a program where the user should be able to write text in a TextBox. I d like the TextBox to resize itself, so it fits to the content. I ve tried the following:

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

I get an error saying that Graphics doesn t work for TextChangedEventArgs. Is there another way I can resize the 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(

我在补充这一回答,因为我看不见正在讨论的案文箱中文本的<编码>固定宽度/代码>方面。 如果你对文本箱有固定的宽度,而且你只想调整其高度,你可以做如下事情:

与此类似,案文的顶点是多线文字框本身是如何选取的:

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

我不相信<条码>标准格式应当只是数值<条码>。 StringFormatFlags 似乎没有适用于构成违约的TextBox

现在有<密码>。 Height,你知道文本箱中文本的高度。

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

我使用了拉贝尔控制区的自治财产。 我在我的表格中添加了一个看不见的标签,确定了其不动产的自治。 当我需要改变文本Box一的大小时,使用该代码:

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

I set the Maximum and Minimum Size of the label for better results. Have Fun

你对错误事件具有约束力,你不能在<代码>中使用图象标。 页: 1

3. 利用文本更改后的活动。 The following snippet is working:

public Form1()
{
    InitializeComponent();

    this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
iii

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

    // Resize the textbox 
    this.textBox1.Width = (int)Math.Round(mySize.Width, 0);
iii

iii

first, create method to Make the TextBox fit its contents.

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

then with the TextChanged event handler calls AutoSizeTextBox() function to make the TextBox fit its text when the text changes.

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

这完全是为了:

resize-a-text Box-to-fit-its-text

你们需要使用形式中的“格拉德”(Graphics)方法,以创建图象,衡量地貌。

<代码>TextChangedEventArgs 类别没有<代码>Graphics property,即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 Object for the form,然后根据文本Box的案文和字体加以测量。 <>代码>using将适当处理Graphics Object---阁下先前的代码本将hor!

无论目标是什么。

如果案文箱的大小应当根据插图动态确定,该插图应当是本箱内的案文,那么there就不是冰箱。

Reasons : MeasureString uses usual string formatters as delimiters for its own width and height. Means, carriage return and line feed are parsed, too. Resulting in a sizeF.Width and sizeF.Height.

这些变量可视体(及其线体和线数)而定,具有价值,这些数值有时是无用的,作为文字箱的宽度/载量值(因为they可能大于括号的数值,因此将文字箱变成一个大小,其左边和底层边界超出母体的)。

根据目标,仍有一些解决办法,希望实现。

One idea would be : Create a textbox in designer, size = 100 X 100. enable word-wrapping.

在文本箱的<>网上展台活动手里,我们只是将案文箱贴上我们自己界定的价值(如括号)。 贴现或其他硬价值......。

www.un.org/Depts/DGACM/index_spanish.htm 这将使文字框中的插图重新编号,这将重新编排文本框内的所有特性,因为文字总结是能够做到的。

文本箱的高度可能会被硬化为括号。 例如,见面。

BUT, better : set the height dynamically,based on the Y value of the ReturnValue (Point) of the method texbox.GetPositionFromCharIndex(textbox.TextLength -1 ). Then, with Math.Min() determine, which is smaller ( either parentform.Height or Point.Y ) , and reset the textbox size to new Size(previousDeterminedWidth, nowDeterminedHeight).

请铭记(如果允许发烧)在计算出的宽度时增加17个。

最佳做法

Did you try to set yourTextBox.AutoSize = true;? This property may be hidden in the GUI designer, but you can set it in the form constructor right after InitializeComponent(); call.

图表。 页: 1 文本Changed EventArgs

我认为,你想要的是:

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

The problem is that you just cannot create a Graphics object by simply allocating it since it has no public constructor, so you should better go and use TextRenderer.MeasureText, as done in http://msdn.microsoft.com/en-us/library/y4xdbe66.aspx

文本Renderer的准确性较低,因为它使用GDI和图表,使用GDI+,因此,或许你应当对你从Width财产获得的价值留下一个很小的差值。

Hope this helps





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签