C# Resize textbox to fit content

前端 未结 10 682
礼貌的吻别
礼貌的吻别 2020-12-05 13:39

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 fit the content. I\'ve tried the following:

相关标签:
10条回答
  • 2020-12-05 14:14

    I am adding this answer as I do not see the fixed width aspect of a textbox being discussed in any of the other. If you have a fixed width for your textbox, and you want to adjust only its height you can do something like the following:

    Something like this gives the height of the text as how it is drawn in the multiline wordwrapped textbox itself:

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

    I am not sure what StringFormat should be but the values StringFormatFlags do not seem to apply to a default TextBox make up.

    Now with MessageSize.Height you know the height of the text in the textbox.

    0 讨论(0)
  • 2020-12-05 14:21

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

    That’s all, for more info:

    resize-a-textbox-to-fit-its-text

    0 讨论(0)
  • 2020-12-05 14:22

    Your binding to the wrong event, and you cannot use the graphics object in the TextChangedEventArgs object.

    Try using the TextChanged event. The following snippet is working:

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

    }

    0 讨论(0)
  • 2020-12-05 14:26

    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.

    0 讨论(0)
提交回复
热议问题