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!
Asked
Active
Viewed 430 times
3 Answers
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;
}
}
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