Adjusting button height by font size

B

Bilz

All,

I came across some code that doesn't work very well. It adjusts the
size of a button based on the font that we use. The font can be
adjusted by the user.

using (Graphics g = this._btnClose.CreateGraphics())
{
this._btnClose.Height = (int)(g.MeasureString(this._btnClose.Text,
this._btnClose.Font).Height * 1.25);
}

This is not enough, since the text is being cut off at the bottom (up
to 2 pixels). To be honest, a factor of 1.25 seems rather arbitrary
to me. I don't like this code for other reasons... one being that a
value of 25.99 will yield a value of 25.

I figure there is probably a better way. Any suggestions?

Thanks,
Brian
 
H

Herfried K. Wagner [MVP]

Bilz said:
I came across some code that doesn't work very well. It adjusts the
size of a button based on the font that we use. The font can be
adjusted by the user.

using (Graphics g = this._btnClose.CreateGraphics())
{
this._btnClose.Height = (int)(g.MeasureString(this._btnClose.Text,
this._btnClose.Font).Height * 1.25);
}

This is not enough, since the text is being cut off at the bottom (up
to 2 pixels). To be honest, a factor of 1.25 seems rather arbitrary
to me. I don't like this code for other reasons... one being that a
value of 25.99 will yield a value of 25.

I figure there is probably a better way. Any suggestions?

Well, why not use a larger scaling factor?
 
B

Bilz

All,

I came across some code that doesn't work very well. It adjusts the
size of a button based on the font that we use. The font can be
adjusted by the user.

using (Graphics g = this._btnClose.CreateGraphics())
{
this._btnClose.Height = (int)(g.MeasureString(this._btnClose.Text,
this._btnClose.Font).Height * 1.25);

}This is not enough, since the text is being cut off at the bottom (up
to 2 pixels). To be honest, a factor of 1.25 seems rather arbitrary
to me. I don't like this code for other reasons... one being that a
value of 25.99 will yield a value of 25.

I figure there is probably a better way. Any suggestions?

Thanks,
Brian

Ok, I figured it out. Instead of that funky math, I just set AutoSize
to true, and AutoSizeMode to GrowAndShrink. This took care of it for
me.

Thanks,
B
 

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