Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find the number of twips in a string.

Posted on 2000-02-25
16
Medium Priority
?
301 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
Suggested Courses

715 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