Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2003, 08:33 AM
Authorized User
 
Join Date: Nov 2003
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to obtain String length in twips


Various fonts generate variable sized strings (in twips). Is there a way (VB function, etc.) to "get" the actual size of a string in twips ?

This might be useful for properly displaying the string in a multi-line cell...

Anyone have a clue ?



 
Old December 11th, 2003, 09:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

I've used an invisible label control for this purpose. Set the label's font and caption, then retrieve the width property.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 11th, 2003, 09:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

I believe there are 1440 twips per inch. As for fonts, they're not in twips, they're in points. A font size of 72 points = 1 inch.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 11th, 2003, 12:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:I've used an invisible label control for this purpose. Set the label's font and caption, then retrieve the width property.
As was pointed out to me in an email, you also need to set the label's autosize property to true for this to work. I forgot that detail.
Quote:
quote:
As for fonts, they're not in twips, they're in points
While it is true that a font is measured in points, it's helpful to know how wide some text is when it is rendered in a given font (e.g. a bold font can be wider than a normal font) and font size, and this invisible label trick will do that.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 11th, 2003, 01:50 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

How about this? If it's for a label, for example, then you can do this:
Code:
Dim sngWidthInches as Single

Me.lblMyLabel.SizeToFit
sngWidthInches = Me.lblMyLabel.Width / 1440
The .SizeToFit method will resize the label to fit the text in question, then you can get the new width using the .Width property.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 11th, 2003, 01:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

maybe this is better..

from msdn


TextWidth Method


Returns the width of a text string as it would be printed in the current font of a Form, PictureBox, or Printer. Doesn't supportnamed arguments.

Syntax

object.TextWidth(string)

The TextWidth method syntax has these parts:

Part Description
object Optional. Anobject expression that evaluates to an object in the Applies To list. If object is omitted, the Form with thefocus is assumed to be object.
String Required. Astring expression that evaluates to a string for which the text width is determined. Parentheses must surround the string expression.


Remarks

The width is expressed in terms of the ScaleMode property setting or Scale method coordinate system in effect for object. Use TextWidth to determine the amount of horizontal space required to display the text. If string contains embedded carriage returns, TextWidth returns the width of the longest line.


this can be use like

me.textwidth("example")


Gonzalo Bianchi





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fix string length Ivanchan Excel VBA 2 July 26th, 2007 08:55 AM
string-length problem alapati.sasi XSLT 3 July 4th, 2007 10:08 AM
Chap 3, "Length of String" richajos BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 February 25th, 2006 08:38 PM
Changing length of string automatically ryanvdm2 Classic ASP Basics 5 September 4th, 2005 06:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.