Solved

Find the number of twips in a string.

Posted on 2000-02-25
16
299 Views
Last Modified: 2012-06-21
Hi,
  I want to find how many Twips there is in a string like in a textBox. Then function (Len) give only the number of caracters, but i need the numbers of twips to ajust the textBox to the String.
0
Comment
Question by:dosyl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +1
16 Comments
 
LVL 14

Expert Comment

by:mcrider
ID: 2559064
You can set your form's font attribute to the same as the textbox, (size, bold, etc.) and then use the following:

x = Form1.TextWidth(Text1.Text)
y = Form1.TextHeight(Text1.Text)


Cheers!
0
 
LVL 14

Accepted Solution

by:
mcrider earned 50 total points
ID: 2559069
I meant my comment as the answer... ;-)


Cheers!
0
 
LVL 1

Author Comment

by:dosyl
ID: 2559404
I will give your points, but what i needed it was for a VsFlexGrid, i thinked it was the same property, and textwidth doesn't exist for VsFGrid.

If you know how i'd like to know.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Vbmaster
ID: 2559562
There is a way to calculate the TextWidth using API's and the hWnd of the control, if you really feel you need to I can probably find it somewhere on my harddrive...
0
 
LVL 1

Author Comment

by:dosyl
ID: 2559772
Vbmaster, if it is a short code i will appreciate it and will can give points for, if it is what i want.
Bye.
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2559850
I was wrong, you need a hDC for the control. Here's the API needed...

   Private Type size
      cx As Long
      cy As Long
   End Type

   Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As size) As Long


What the API needs is the hDC, the string, the length of the string, and it will give the size in the lpSize parameter. Here's a example...

   Dim TextSize As size

   Call GetTextExtentPoint32(Me.hDC, "Teststring", 10, TextSize)

   Me.Caption = TextSize.cx & "," & TextSize.cy

(These values are in pixels so if you want 'em in twips you need to divide the values by Screen.TwipsPerPixelX resp Screen.TwipsPerPixelY)
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2559853
In case you do not have a hDC you can create a virtual one using the controls hWnd property (you need at least one of those properties), using the API's...

   Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

   Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long


You use these API's with code like...

   Dim hDC As Long
   hDC = GetDC(controlname.hWnd)


Just remember to clear the hDC when you're done. You do that with...

   Call ReleaseDC(hDC)
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2559859
One small error on the last comment, the hDC function is used with code like...

  ReleaseDC(controlname.hWnd, hDC)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2560022
Using the TextWidth method will work fine.  Set the Form's font to the same font that is in the FlexGrid control.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2560049
Vbmaster, if it is a short code i will appreciate it and will can give points for, if it is what i want.
Bye.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2560056
Erik37, i don't understand,i'd need a little bit of code.
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2560071
Humm.. I already posted the code neccessary for the API version. The version Erick37 talks about is the exact same as mcrider's solution. you use something like Me.Font = VsFlexGrid.Font and that way all text strings will have the same size in the form as in the VsFlexGrid. Then you use TextWidth and/or TextHeight to get the size of a string.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2560080
OK vbMaster.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2560417
I love it when people *completely* change their requirement mid-stream...
Your original question was about textboxes and strings... Not VsFlexGrid.

Still, the concept is exactly the same.

You set the forms font attributes to the VsFlexGrid and then call Form.TextHeight and Form.TextWidth just like I showed in the original example.


Cheers!
0
 
LVL 1

Author Comment

by:dosyl
ID: 2561086
mcrider
   Don't be angry for this, i said i give your points and i said : i thinked it was the same thing for a grid and it is not. I'll give points to vbMaster when i'll test is code if it is work for my grid.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2589420
The very good answer is :
x = Form1.TextWidth(VSFlexGrid1.Text)
or
x = Form1.TextWidth(VSFlexGrid1.TextMatrix(5,2))
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 connector to SQL Server 2 42
VBA - If Bookmark = "XXBOOKMARKXX" then 15 67
VBA: loop recent folder and copy txt file. 8 56
SQL Server 2012 to SQL Server 2016 24 57
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question