Solved

Find the number of twips in a string.

Posted on 2000-02-25
16
298 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

830 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