PC Review


Reply
Thread Tools Rate Thread

Adjusting button height by font size

 
 
Bilz
Guest
Posts: n/a
 
      29th Jan 2007
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

 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      29th Jan 2007
"Bilz" <(E-Mail Removed)> schrieb:
> 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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
Bilz
Guest
Posts: n/a
 
      29th Jan 2007
On Jan 29, 1:33 pm, "Bilz" <BrianGeni...@gmail.com> wrote:
> 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

 
Reply With Quote
 
Bilz
Guest
Posts: n/a
 
      29th Jan 2007
On Jan 29, 2:00 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
>Well, why not use a larger scaling factor?
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


You're kidding, right? Hard coding a "magic value" like that is sure
to fail again down the road again.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adjusting Font Size Question Don Windows Vista General Discussion 1 30th Sep 2008 09:38 PM
auto adjusting cell size and row height daphoenix Microsoft Excel Programming 3 10th Jul 2008 04:23 PM
Problems with adjusting font size Disclaimer777 Microsoft Outlook Discussion 3 24th Apr 2007 10:28 AM
Need the option of adjusting font size for different messages =?Utf-8?B?VW5oYXBweQ==?= Microsoft Access Getting Started 2 31st Oct 2005 05:21 PM
Adjusting Button height crosser Microsoft VB .NET 3 21st Mar 2005 03:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 AM.