truncating strings to fit inside a button

Don't want strings to go out of button, so want to cutoff the stri

Page 1 of 1

3 Replies - 4947 Views - Last Post: 11 February 2008 - 02:44 AM Rate Topic: -----

#1 RobinJet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 08

truncating strings to fit inside a button

Post icon  Posted 04 February 2008 - 02:08 PM

Hi all!

Problem Intro
I have a button in VB.NET where I have a long Button Label.
(I'm using VS 2008 incase you need to know, don't worry its quite like VS 2003/VS 2005)

The problem that the Label of the button is taken from an external source which can have as many as 100 chars or more!

So I want to truncate it and show
"My very large button label name 1 2 3"
as
"My very lar..."

Usual Solution
Now most people use a char count limit:
like
 myLongLabel.substring(0,10) 


But I don't want to use a monospaced font but instead a font where 'i' or 'l' is much thinner than 'O' or 'W'

So lets say I have a 10 char limit, becuase of that my button will look like

"iiiiiiiiii..."
"WWWWWWWWWW..."

But you see in actuality the ten W's are much wider than the ten 'i's.

Propsed Solution

One way would be to find the actual width of each character by looking up the font in question. How can I get the width of the characters of installed fonts? Any API in .NET 2.0 or .NET 3.5?

Or even any external library would do.
Maybe even some strange command-line EXE that returns the char as a bitmap - then I can read the width of that char as a bitmap and make a LUT once and for all..... then constantly use that LUT to tell me which char is how wide.

I know how wide my button is so I can just keep adding chars as long as they fill up the button!

This post has been edited by RobinJet: 04 February 2008 - 02:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: truncating strings to fit inside a button

#2 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 31
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: truncating strings to fit inside a button

Posted 04 February 2008 - 09:50 PM

Try Graphics.MeasureString

Here is an example:
Bitmap temp = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(temp);
SizeF sf = g.MeasureString("sometext", btnAddLanguage.Font);
MessageBox.Show(sf.Width.ToString());



Hope it helps.
Was This Post Helpful? 0
  • +
  • -

#3 RobinJet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 08

Re: truncating strings to fit inside a button

Posted 11 February 2008 - 12:27 AM

Wow great!

I did'nt even know such a function exists.
Thanks so much!

I needed this in multiple apps so this'll really make a difference to many fronts as such.
Was This Post Helpful? 0
  • +
  • -

#4 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: truncating strings to fit inside a button

Posted 11 February 2008 - 02:44 AM

A much easier and more conventional way to do this is to enable the AutoEllipsis property on your button control.

For more information read 2.09 in my button tutorial (click).

Additional information can be found on MSDN here (click).

Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1