0

My text title may be of different length, and I want that text fully fit within the entire title bar of a form. So I need to change width of a form. But how to know a width of a text in a title, when in different systems font and size of title text can be very different? Thanks in advance!

Anatolii Humennyi
  • 1,807
  • 3
  • 26
  • 37
  • 2
    See [here](http://stackoverflow.com/questions/18089280/adapt-the-size-of-a-form-to-its-title-text-in-c-sharp). – Andrei V Nov 11 '13 at 11:12

3 Answers3

2

May be this could help,

    private void YourFormName_SizeChanged(object sender, EventArgs e)
    {
        this.Width = 300; // the default or initial width
    }

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.Handled = true;

           if (textBox1.Text.Length == 0)
            {
                this.Width = 300;
                goto END;
            }
            if (textBox1.Text.Length < this.Width)
                this.Width = textBox1.Text.Length;
            else
                this.Width = this.Width + textBox1.Text.Length;
        END:this.Text = textBox1.Text;
        }
    }
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
manish
  • 1,450
  • 8
  • 13
1

In WPF that is resolved easy, but as for Windows Forms, I think you may inherit from standard Window control and in the initialization code assign header title width (it must be simple to calculate, as Andrei V mentioned in comments) to a form.

0

In the form, Put the following code in the constructor:

this.Width = this.Width + this.Text.Length;

this.text returns the text of the title. this.width returns the width of the form. Just adjust the width according to the length of the text in the title.

Wasiq Ali
  • 251
  • 2
  • 11