|
-
Jun 16th, 2004, 09:03 AM
#1
Thread Starter
Lively Member
Autosize label
Is it possible to have a labels width fixed (1335 twips) and have it´s height autosized?
I want the labels height to change dynamicly at runtime. I´ve tried all kinds of solutions, but nothing seems to work for me.
-
Jun 16th, 2004, 10:02 AM
#2
You may use Multiline textbox with Locked = True instead.
-
Jun 16th, 2004, 10:29 AM
#3
Thread Starter
Lively Member
Originally posted by RhinoBull
You may use Multiline textbox with Locked = True instead.
Hmm ok i don´t think i understand I want the label (or textbox doesn´t matter) to resize to fit the text so you can see all the text without the need to scroll.
-
Jun 16th, 2004, 11:50 AM
#4
Banned
wow thats hard! set the property.. to autosize, true.. wow how hard...[/sarcasm]
-
Jun 16th, 2004, 11:58 AM
#5
It's not about autosize at all and here is a sample:
create a textbox with Multiline = True and no Scrollbars and run this code (modified version of Randy's sample):
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Sub Form_Load()
Me.Font = Text1.Font
End Sub
Private Sub Text1_Change()
'==========================
Dim h As Long
Dim lineCount As Long
On Local Error Resume Next
lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
h = TextHeight("A") * lineCount
If Text1.Height < h Then Text1.Height = h + 120
End Sub
-
Jun 16th, 2004, 12:04 PM
#6
You don't need textbox! Just set label's both WordWrap and Autosize = True, make the label correct width and it autosizes just like you described
-
Jun 16th, 2004, 01:32 PM
#7
Label isn't always behave as expected so that's why textbox with Locked and Multiline = True comes very handy.
-
Jun 16th, 2004, 01:37 PM
#8
Show me an example when wordwrapped autosized label doesn't work as it should, as for me it has always worked fine.
-
Jun 16th, 2004, 02:16 PM
#9
Thread Starter
Lively Member
Originally posted by Merri
Show me an example when wordwrapped autosized label doesn't work as it should, as for me it has always worked fine.
Private Sub Command1_Click()
Label1.AutoSize = True
Label1.WordWrap = True
Label1.Width = 1335
Label1.Caption = "mksadlaskdlaksmasertgrtgertgrtgertgertgd "
MsgBox Label1.Width
End Sub
Run this code, what does the msgbox show? My msgbox shows 3045 and i want it to show 1335
-
Jun 16th, 2004, 02:25 PM
#10
Thread Starter
Lively Member
Originally posted by RhinoBull
It's not about autosize at all and here is a sample:
create a textbox with Multiline = True and no Scrollbars and run this code (modified version of Randy's sample):
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Sub Form_Load()
Me.Font = Text1.Font
End Sub
Private Sub Text1_Change()
'==========================
Dim h As Long
Dim lineCount As Long
On Local Error Resume Next
lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
h = TextHeight("A") * lineCount
If Text1.Height < h Then Text1.Height = h + 120
End Sub
That was a great example! I´m gonna use that code.
-
Jun 16th, 2004, 02:33 PM
#11
Originally posted by john42
That was a great example! I´m gonna use that code.
Cool !
-
Jun 16th, 2004, 02:40 PM
#12
Ah, that case. I've always used "proper" text when I've used wordwrapped text. Anyways, you can always have wordwrapped label and do this:
VB Code:
Option Explicit
Private Function FormatWrap(ByVal Text As String, InWidth As Long) As String
Dim Temps() As String, Temp As String
Dim A As Integer, B As Integer
Temps = Split(Text, " ")
For A = 0 To UBound(Temps)
If TextWidth(Temps(A)) < InWidth Then
Temp = Temp & " " & Temps(A)
Else
B = Len(Temps(A))
Do While B
B = B - 1
If TextWidth(Left$(Temps(A), B)) < InWidth Then
Temp = Temp & " " & Left$(Temps(A), B)
If Not B = Len(Temps(A)) Then
Temps(A) = Mid$(Temps(A), B + 1)
B = Len(Temps(A)) + 1
Else
B = 0
End If
End If
Loop
End If
Next A
FormatWrap = Mid$(Temp, 2)
End Function
Private Sub Form_Load()
Label1 = FormatWrap("Mgfdhgkjdhgkdhhdk", Label1.Width)
End Sub
I'm too generous for making all these codes!
-
Jun 16th, 2004, 03:11 PM
#13
I would assume it works but very inefficient - loop within another loop ... plus all of those string functions, array, vars ...
-
Jun 16th, 2004, 03:13 PM
#14
I know, I were too lazy to start converting to byte array and loop through it and convert back to string It works fast enough for single purpose needs though.
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
|