Results 1 to 14 of 14

Thread: Autosize label

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    ja
    Posts
    82

    Unhappy 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.
    Hej på dej!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You may use Multiline textbox with Locked = True instead.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    ja
    Posts
    82
    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.
    Hej på dej!

  4. #4
    wow thats hard! set the property.. to autosize, true.. wow how hard...[/sarcasm]

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    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:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" _
    4.    Alias "SendMessageA" _
    5.   (ByVal hwnd As Long, _
    6.    ByVal wMsg As Long, _
    7.    ByVal wParam As Long, _
    8.    lParam As Any) As Long
    9.  
    10. Private Const EM_GETLINECOUNT = &HBA
    11.  
    12. Private Sub Form_Load()
    13.     Me.Font = Text1.Font
    14. End Sub
    15.  
    16. Private Sub Text1_Change()
    17. '==========================
    18. Dim h As Long
    19. Dim lineCount As Long
    20.  
    21. On Local Error Resume Next
    22.  
    23.     lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
    24.     h = TextHeight("A") * lineCount
    25.     If Text1.Height < h Then Text1.Height = h + 120
    26.  
    27. End Sub

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Label isn't always behave as expected so that's why textbox with Locked and Multiline = True comes very handy.

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Show me an example when wordwrapped autosized label doesn't work as it should, as for me it has always worked fine.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    ja
    Posts
    82
    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
    Hej på dej!

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    ja
    Posts
    82
    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:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" _
    4.    Alias "SendMessageA" _
    5.   (ByVal hwnd As Long, _
    6.    ByVal wMsg As Long, _
    7.    ByVal wParam As Long, _
    8.    lParam As Any) As Long
    9.  
    10. Private Const EM_GETLINECOUNT = &HBA
    11.  
    12. Private Sub Form_Load()
    13.     Me.Font = Text1.Font
    14. End Sub
    15.  
    16. Private Sub Text1_Change()
    17. '==========================
    18. Dim h As Long
    19. Dim lineCount As Long
    20.  
    21. On Local Error Resume Next
    22.  
    23.     lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
    24.     h = TextHeight("A") * lineCount
    25.     If Text1.Height < h Then Text1.Height = h + 120
    26.  
    27. End Sub
    That was a great example! I´m gonna use that code.
    Hej på dej!

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Originally posted by john42
    That was a great example! I´m gonna use that code.
    Cool !

  12. #12
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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:
    1. Option Explicit
    2. Private Function FormatWrap(ByVal Text As String, InWidth As Long) As String
    3.     Dim Temps() As String, Temp As String
    4.     Dim A As Integer, B As Integer
    5.     Temps = Split(Text, " ")
    6.     For A = 0 To UBound(Temps)
    7.         If TextWidth(Temps(A)) < InWidth Then
    8.             Temp = Temp & " " & Temps(A)
    9.         Else
    10.             B = Len(Temps(A))
    11.             Do While B
    12.                 B = B - 1
    13.                 If TextWidth(Left$(Temps(A), B)) < InWidth Then
    14.                     Temp = Temp & " " & Left$(Temps(A), B)
    15.                     If Not B = Len(Temps(A)) Then
    16.                         Temps(A) = Mid$(Temps(A), B + 1)
    17.                         B = Len(Temps(A)) + 1
    18.                     Else
    19.                         B = 0
    20.                     End If
    21.                 End If
    22.             Loop
    23.         End If
    24.     Next A
    25.     FormatWrap = Mid$(Temp, 2)
    26. End Function
    27. Private Sub Form_Load()
    28.     Label1 = FormatWrap("Mgfdhgkjdhgkdhhdk", Label1.Width)
    29. End Sub

    I'm too generous for making all these codes!

  13. #13
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    I would assume it works but very inefficient - loop within another loop ... plus all of those string functions, array, vars ...

  14. #14
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    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
  •  



Click Here to Expand Forum to Full Width