Solved

Find the number of twips in a string.

Posted on 2000-02-25
16
294 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
Comment Utility
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
Comment Utility
I meant my comment as the answer... ;-)


Cheers!
0
 
LVL 1

Author Comment

by:dosyl
Comment Utility
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
 
LVL 7

Expert Comment

by:Vbmaster
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
One small error on the last comment, the hDC function is used with code like...

  ReleaseDC(controlname.hWnd, hDC)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Expert Comment

by:Erick37
Comment Utility
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
Comment Utility
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
Comment Utility
Erik37, i don't understand,i'd need a little bit of code.
0
 
LVL 7

Expert Comment

by:Vbmaster
Comment Utility
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
Comment Utility
OK vbMaster.
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
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
Comment Utility
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
Comment Utility
The very good answer is :
x = Form1.TextWidth(VSFlexGrid1.Text)
or
x = Form1.TextWidth(VSFlexGrid1.TextMatrix(5,2))
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now