How to ensure Form title visibility?

  • Thread starter Thread starter Paolo Pagano
  • Start date Start date
P

Paolo Pagano

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
 
Hi Paolo,

In principle you could just do a Graphics.MeasureString to find the width
of the string, but finding the exact sizes of the buttons may be trickier.

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 size
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 = this.CreateGraphics())
{
SizeF size = g.MeasureString(this.Text,
SystemFonts.CaptionFont);
int width = (int)size.Width;
if(MaximizeBox)
width += 20;
if (MinimizeBox)
width += 20;
if (ControlBox)
width += 30;
width += 30;

this.MinimumSize = new Size(width,
this.MinimumSize.Height);
}
base.OnTextChanged(e);
}
 
Back
Top