944,001 Members | Top Members by Rank

Ad:
Sep 8th, 2005
0

Text Width

Expand Post »
Hi,

Is it possible to measure the width of a line of text saved into a variable.

i.e.
strData = "XXXXXX"
length = 12

strData = "iiiiii"
length = 4

Any font can be used as the data isn't going to be output on screen.

The reason:
I have a csv file holding various names and addresss, I am wanting to find the longest of each of the address fields. I currently have a program that counts the number of characters in a field but I am after the longest in size as well.

XXXX might less characters than iiiiii but it is longer.

Cheers for any help
pG :eek:
Similar Threads
Reputation Points: 24
Solved Threads: 6
Junior Poster in Training
purplegerbil is offline Offline
78 posts
since Apr 2005
Sep 9th, 2005
0

Re: Text Width

I have got one solution but it is too slow.

Set a label to auto size and copy the data into the label.
check the label.width value.

This works fine but as i said above, its too slow. The application is going to process between 250,000 and 10 million records.

pG
Reputation Points: 24
Solved Threads: 6
Junior Poster in Training
purplegerbil is offline Offline
78 posts
since Apr 2005
Sep 9th, 2005
0

Re: Text Width

Debug.Print TextWidth("XXXXX"), TextWidth("iiiii")
Reputation Points: 20
Solved Threads: 5
Junior Poster
jwshepherd is offline Offline
123 posts
since Jun 2005
Sep 12th, 2005
0

Re: Text Width

This is the code I am using. This speeds up my app by 50% because the resulst dont need to go to the screen/form.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
  2. Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
  3.  
  4. Private Type POINTAPI
  5. X As Long
  6. Y As Long
  7. End Type
  8.  
  9. Private Function text_length(Mytext As String) As Long
  10. Dim TextSize As POINTAPI
  11. GetTextExtentPoint32 GetWindowDC(hwnd), Mytext, Len(Mytext), TextSize
  12.  
  13. text_length = TextSize.X
  14. End Function

pG
Reputation Points: 24
Solved Threads: 6
Junior Poster in Training
purplegerbil is offline Offline
78 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Picture Size problem
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Crystal REport 9.0





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC