Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Text Width 1

Status
Not open for further replies.

nvwildfire

Technical User
Aug 15, 2002
43
0
0
US
My dilemma is that the software I am using (ArcMap 8.2) does not have word wrap for its text elements. So when I have a user enter a bunch of text on a form and then when I try to place that text on a map as a text element (basically a label) the text will extend out until the end of the string is reached, which is generally a couple of inches off of the map page if they enter a paragraph.

What I would like to do is write some code that will through several iterations wrap the text based on a length that I find acceptable. I can not seem to figure out how to determine the actual width of the text given its size and font (i.e. 12 point Arial). Is there a way to get the width of a string or text in a textbox? If I can get past the width part I think I can figure the rest of the code out.

Any help would be greatly appreciated.

Karl Krauter



 
Set Autosize of an Label to TRUE

label1.Caption = "Label1"
MsgBox Str(label1.Width)
label1.Caption = "Label 400"
MsgBox Str(label1.Width)

You will see that the wigth is changing. now you can determinate your width and you will do

for i = 12 to 8
label1.fontsize = i
if label1.width < YouSpace then exit for
next i


peterguhl@yahoo.de
 
If the textbox has the same font as the form then use the TextWidth method. The width is returned according to the ScaleMode setting:

intTextWidth = TextWidth(Text1.Text)

Paul Bent
Northwind IT Systems
 

Hi Karl:

One can use the TextWidth and TextHeight properties to determine word wrap. If the specific control (textbox or label) doesn't have these properties, one use the TextWidth and TextHeight properties of the form (for example,
Code:
Me.TextWidth(strMyText)
.)

To do word wrap, set up a loop to scan the source string for whitespace (spaces and tabs). Then add each word from the source string to a destination string until the textwidth exceeds the desired limit. Back up one word, place a vbCRLF character in the destination string and continue adding words until one hits the right margin again. Loop until done.

Sorry. I don't have sample code handy.

Cassie
;-)
 
Thanks everyone for the quick response. I am now starting to realize what my problem is. I am using VBA instead of VB. I am thinking that textwidth is not available in VBA? Can anyone confirm this? Thanks in advance.

kgk
 
The TextWidth property is available in VBA, but only for a Report Object. You'll need to load a report, set the font parameters as necessary, and then use the TextWidth from within the report to get the values.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
VB's TextWidth and TextHeight properties are just wrappers for the GetTextExtent32 API call, which is usable in VB and VBA
 
CajunCenturion,

Thanks for the input.

To add a report object do I assume I have to add the report control to my tool box? I have done so but the icon it adds just say OCX (doesn't have an icon that looks to have any meaning) and gives a .dll error when I try to add it to my form? Any thoughts on this?

Strongm,

thanks for the information, being a relative newbie I really don't know anything about making API calls. I'll need to do some reading on the subject.

kgk

 
Here you'll have a example


Private Type POINTAPI

X As Long
Y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function WindowFromPoint Lib &quot;user32&quot; (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib &quot;user32&quot; (lpPoint As POINTAPI) As Long
Private Declare Function ExtTextOut Lib &quot;gdi32&quot; Alias &quot;ExtTextOutA&quot; (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal wOptions As Long, ByVal lpRect As Any, ByVal lpString As String, ByVal nCount As Long, lpDx As Long) As Long
Private Declare Function GetWindowRect Lib &quot;user32&quot; (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetTextExtentPoint32 Lib &quot;gdi32&quot; Alias &quot;GetTextExtentPoint32A&quot; (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As POINTAPI) As Long
Private Declare Function GetWindowDC Lib &quot;user32&quot; (ByVal hwnd As Long) As Long
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Pt As POINTAPI, mWnd As Long, WR As RECT, nDC As Long
Dim TextSize As POINTAPI, CX As Long, CY As Long
'Get the current cursor position
GetCursorPos Pt
'Get the window under the cursor
mWnd = WindowFromPoint(Pt.X, Pt.Y)
'Get the window's position
GetWindowRect mWnd, WR
'Get the window'zs device context
nDC = GetWindowDC(mWnd)
'Get the height and width of our text
GetTextExtentPoint32 nDC, &quot;Hello !&quot;, Len(&quot;Hello !&quot;), TextSize
For CX = 1 To WR.Right - WR.Left Step TextSize.X
For CY = 1 To WR.Bottom - WR.Top Step TextSize.Y
'Draw the text on the window
ExtTextOut nDC, CX, CY, 0, ByVal 0&, &quot;Hello !&quot;, Len(&quot;Hello !&quot;), ByVal 0&
Next
Next
End Sub
Private Sub Form_Paint()
Me.CurrentX = 0
Me.CurrentY = 0
Me.Print &quot;Click on this form,&quot; + vbCrLf + &quot;Hold the mouse button,&quot; + vbCrLf + &quot;drag the mouse over another window,&quot; + vbCrLf + &quot;release the mouse button&quot; + vbCrLf + &quot;and see what happens!&quot;
End Sub


peterguhl@yahoo.de
 
For the sake of diversity, here is another way to wrap text:

Code:
Public Function WrapText(TextToWrap As String, FontName As String, FontSize As Integer, FontBold As Boolean, Inches As Double) As String
    Dim LCV As Long
    Dim Words As Variant
    Dim TextLength As Double
    Dim TempWrapText As String
    
    Printer.ScaleMode = 5 'set to inches
    Printer.Font = FontName
    Printer.FontSize = FontSize
    Printer.FontBold = FontBold
    
    TextToWrap = Replace(TextToWrap, Chr(13), &quot;  &quot;)
    
    Words = Split(TextToWrap, &quot; &quot;)
    
    TextLength = 0
    
    For LCV = 0 To UBound(Words)
        If TextLength + Printer.TextWidth(Words(LCV)) <= Inches Then
            TempWrapText = TempWrapText & Words(LCV) & &quot; &quot;
            TextLength = Printer.TextWidth(TempWrapText)
        Else
            Exit For
        End If
    Next
    
    WrapText = TempWrapText
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top