Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Find the number of twips in a string.

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
dosyl
Asked:
dosyl
  • 7
  • 5
  • 3
  • +1
1 Solution
 
mcriderCommented:
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
 
mcriderCommented:
I meant my comment as the answer... ;-)


Cheers!
0
 
dosylAuthor Commented:
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
Technology Partners: 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!

 
VbmasterCommented:
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
 
dosylAuthor Commented:
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
 
VbmasterCommented:
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
 
VbmasterCommented:
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
 
VbmasterCommented:
One small error on the last comment, the hDC function is used with code like...

  ReleaseDC(controlname.hWnd, hDC)
0
 
Erick37Commented:
Using the TextWidth method will work fine.  Set the Form's font to the same font that is in the FlexGrid control.
0
 
dosylAuthor Commented:
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
 
dosylAuthor Commented:
Erik37, i don't understand,i'd need a little bit of code.
0
 
VbmasterCommented:
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
 
dosylAuthor Commented:
OK vbMaster.
0
 
mcriderCommented:
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
 
dosylAuthor Commented:
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
 
dosylAuthor Commented:
The very good answer is :
x = Form1.TextWidth(VSFlexGrid1.Text)
or
x = Form1.TextWidth(VSFlexGrid1.TextMatrix(5,2))
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now