Groups | Blog | Home
all groups > dotnet windows forms > october 2006 >

dotnet windows forms : How to ensure Form title visibility?


Paolo Pagano
10/12/2006 1:23:28 PM
Showing a form as:

Form f = new Form();
f.Text = "Very very very very very very very long title text...";

f.Width = ???;

f.howDialog();


is there a safe way to set the form Width so that it's text is fully
visible?
This way should take in account title bar font, system icon,
maximize/minimize boxes
wich can or cannot be visible.

thanks

Morten Wennevik
10/12/2006 1:56:47 PM
Hi Paolo,

In principle you could just do a Graphics.MeasureString to find the widt=
h =

of the string, but finding the exact sizes of the buttons may be trickie=
r.

The code below might give you some ideas. It will adjust the form's =

minimumwidth when the Text property changes. It will also adjust the si=
ze =

depending on the visibility of boxes. It may be that the CloseBox is =

always visible as long as the form has a border, though without a border=
=

there would be no caption.

protected override void OnTextChanged(EventArgs e)
{
using (Graphics g =3D this.CreateGraphics())
{
SizeF size =3D g.MeasureString(this.Text, =

SystemFonts.CaptionFont);
int width =3D (int)size.Width;
if(MaximizeBox)
width +=3D 20;
if (MinimizeBox)
width +=3D 20;
if (ControlBox)
width +=3D 30;
width +=3D 30;

this.MinimumSize =3D new Size(width, =

this.MinimumSize.Height);
}
base.OnTextChanged(e);
}

-- =

Happy Coding!
AddThis Social Bookmark Button