Thus far I have always chosen the font Courier New because all letters are the same width. Is there any way of determining how "long" a string will be when using other fonts? I would like a way of checking the string's actual length so I can format a printout properly when using fonts that have letters with varying widths.

Recommended Answers

All 4 Replies

What programming language are you using?
You could use

http://us2.php.net/manual/en/function.strlen.php

And then create an if statement to use this display with this font or that display with another font based on length.

Example:

<?php
$str = 'abcdef';
echo strlen($str); 
if ($str >= 5){
?>

<font face="arial">Arial font because its greater then or equal 5</font>

<?
} else {
?>

<font face="tahoma">Tahoma font because its less then 5</font>

<?
}
?>

You mean this one ?

Private Sub Form_Load()
    Me.FontName = "Verdana"
    MsgBox (Me.TextWidth("Hello"))
End Sub

Man I am two for two today!!

HAHA Posted PHP code in VB just now.. and earlier I posted Javascript in C#

I am calling it a day some one stick a fork in me...

Yes, TextWidth is the one! Thanks. I should have searched VB help a bit more before posting the question. Oh well.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.