Determine exact string width (in pixels)

perlfan

Registered User.
Local time
Today, 07:28
Joined
May 26, 2009
Messages
192
Hi there,

I am working with the Access Treeview feature and I'd like to align the different elements in my nodes exactly below each other.

Therefore I need exact distances (if string is longer then XY, cut short, if shorter, extend to XY).

Of course I cannot work with len() and fill with spaces because letters have different widths.

Is there a way to determine the exact width of a string? e.g. in pixels?

Thanks for help - FRANK
 
have a look at stephen lebans's cangrow function. It has the code which calculates width and height a control needs to be allowing for font, font size etc.

http://www.lebans.com/cangrow.htm

You can adapt this for your purposes
 
Fantastic - I'm using the fTextWidth function and the results are quite precise. Thanks a lot!
 
i'm amazed that you can do that. Stephen Lebans did some very clever and useful stuff, didn't he?
 
Just a short follow up question: function fTextWidth seams to ignore "&" which leads to unprecise results in case this character shows up. Can somebody help?
 
you mean '&' the text?

Probably because & is a character used in code

Just a suggestion, and will need some experimenting, you could try changing the & character with one or more alternative characters to represent the space - perhaps the 8 character? before calculating the dimensions.

So assign a string variable the value of the text, replacing & with 8 and pass the string variable as a parameter to the cangrow function
 
Thx CJ, no, & is used in a company name. The same applies to -. I had come up with the same approach, but I thought there might be a good explanation and solution for this.... So now I am replacing same characters like & with similar characters like B before I determine the width...
 

Users who are viewing this thread

Back
Top Bottom