How to ensure Form title visibility?

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
 
M

Morten Wennevik

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top