|
-
Mar 9th, 2005, 12:55 PM
#1
Thread Starter
Hyperactive Member
Get the height in pixels of the text in a text box - HardCore?
I Filled a TextBox with a text from a text file... no I want to size the height of the TextBox to the Height of the text.
can It be done? API? Any Ideas?
Thanks in advanced.
-
Mar 9th, 2005, 01:06 PM
#2
Re: Get the height in pixels of the text in a text box - HardCore?
Make sure the forms font is set the same as the textbox and do:
Code:
MsgBox Form1.TextHeight(Text1.Text)
-
Mar 9th, 2005, 01:15 PM
#3
Re: Get the height in pixels of the text in a text box - HardCore?
TextHeight can be used if the string isn't a multiline string. If it is you can still use TextHeight * NumberOfLines. But if the textbox is wordwrapping it might be hard to know the exact number of lines. In that case you can use the DrawText API. Which also could be used to for example make a label autosize vertically.
VB Code:
Private Declare Function DrawText _
Lib "user32.dll" Alias "DrawTextA" ( _
ByVal hdc As Long, _
ByVal lpStr As String, _
ByVal nCount As Long, _
ByRef lpRect As RECT, _
ByVal wFormat As Long) As Long
Private Const DT_CALCRECT As Long = &H400
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub AdjustHeight(txtBox As TextBox)
Dim sText As String
Dim r As RECT
Dim nHeight As Long
'adjust the scale mode for easier calculations
Me.ScaleMode = vbPixels
r.Right = txtBox.Width - 4 ' -4 px for the border, assumes 3d style is used
sText = txtBox.Text
nHeight = DrawText(Me.hdc, sText, Len(sText), r, DT_CALCRECT)
If nHeight Then
txtBox.Height = nHeight + 6
End If
End Sub
-
Mar 9th, 2005, 01:19 PM
#4
Re: Get the height in pixels of the text in a text box - HardCore?
This function will wrap the text and use .TEXTHEIGHT.
Code:
frmTeaMark.TextHeight(Wrap_Text(txtData.Text, txtData.Width))
Code:
Private Function Wrap_Text(ByVal strText As String, lngWidth As Long) As String
Dim x As Long, y As Long, z As Long, s1 As String
s1 = ""
Do While strText <> ""
For x = 1 To Len(strText)
Select Case Asc(Mid$(strText, x, 1))
Case 32
y = x
Case 13
Debug.Print "Got 13"
Case 10
Debug.Print "Got 10"
z = x
End Select
If z <> 0 Then
Debug.Print Left(strText, z - 2)
If s1 <> "" Then s1 = s1 & vbCrLf
s1 = s1 & Left(strText, z - 2)
strText = Mid(strText, z + 1)
z = 0
Exit For
End If
If frmTeaMark.TextWidth(Left(strText, x)) > lngWidth Then
Debug.Print Left(strText, y - 1)
If s1 <> "" Then s1 = s1 & vbCrLf
s1 = s1 & Left(strText, y - 1)
strText = Mid(strText, y + 1)
Exit For
End If
If x = Len(strText) Then
Debug.Print strText
If s1 <> "" Then s1 = s1 & vbCrLf
s1 = s1 & strText
strText = ""
Exit For
End If
Next x
Loop
Wrap_Text = s1
End Function
-
Mar 9th, 2005, 01:28 PM
#5
Re: Get the height in pixels of the text in a text box - HardCore?
 Originally Posted by szlamany
This function will wrap the text and use .TEXTHEIGHT.
All that is fine but a textbox automatically wraps text if you don't show the horizontal scrollbar and in that case the DrawText is the way to go since it does the calculation for you. Less code and faster result.
-
Mar 9th, 2005, 01:32 PM
#6
Re: Get the height in pixels of the text in a text box - HardCore?
 Originally Posted by Joacim Andersson
All that is fine but a textbox automatically wraps text if you don't show the horizontal scrollbar and in that case the DrawText is the way to go since it does the calculation for you. Less code and faster result.
We end up having to store the data in a SQL table - and also having to print it - so wrapping the text and inserting those "assumed" cr/lf's was necessary.
Unfortunately you cannot stop a textbox at a certain height - so when the user leaves the textbox we needed to make sure they didn't put too much in the textbox to fit on the report card...
-
Mar 9th, 2005, 08:50 PM
#7
Hyperactive Member
Re: Get the height in pixels of the text in a text box - HardCore?
if you dont want to do those other ideas,(they scare me) lol
you can use currentx and currenty to calculate it
(currentx and currenty are the co-ordinates of the next print statement on a form/picturebox)
Code:
string = textxox.text
for x = 1 to len(string)
if currenty > (maximum width you want it to go to) then
print mid(string,x,1)
else
print mid(string, x,1);
end if
next x
textbox .height = currentx + (a bit so its not cramped)
the only problem with that is it does not correct for words staying together(if it even matters)
just make sure the form and textbox text settings are the same and you might have to change it slightly to give more extra
It's not just Good, It's Good enough!
Spelling Eludes Me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|