Results 1 to 10 of 10

Thread: Resize a Label's height to fit text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    84

    Resize a Label's height to fit text

    Using VB.NET 2005, Compact Framework 2 for Windows Mobile 5 PDA

    I have a Label on a form that at runtime will have its Text property set to a string from a database. This string is variable in length upto 255 characters. The width of the Label is fixed, but is there a way after the text has been added to resize the Labels height, so it fits the text.

    Thanks

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: Resize a Label's height to fit text

    If you want the new text to nicely fit in the label, you'll have to calculate the area needed to hold the text (based on the text length, font size and in your particular case, the fixed width of the label. In other words, you already have the area's width which is the label's width, you just need to calculate the height)
    VB Code:
    1. Dim w As Integer = Label1.Width
    2. Dim h As Integer = CalculateHeight(strText, Label1.Font.Size) 'How you calculate this
    3. 'is up to you but it's just like how you calculate the print area in a PrintPage event
    4. Dim sz As New Size(w, h)
    5. Label1.Size = sz
    6. Label1.Text = strText

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Resize a Label's height to fit text

    VB Code:
    1. Private Function CalculateHeight() As Single
    2.         Dim g As Graphics = Label1.CreateGraphics
    3.         Return g.MeasureString(Label1.Text, Label1.Font).Width
    4.     End Function
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    84

    Re: Resize a Label's height to fit text

    Thanks.

    I am trying to do this though in the CF, and I don;t seem to have the CeateGraphics function. Is there an alternative for the CF?

    Thanks

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Resize a Label's height to fit text

    try using Me.CreateGraphics and use the form's CreateGraphics method instead of the labels (Since the labels CreateGraphics method is not available in the CF)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    84

    Re: Resize a Label's height to fit text

    I don't understand. If it is the label I want o calculate the height of, how will the CreateGraphics of the form work? And what would I set the text property to?

    If possible could you give an example?

    Thanks

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Resize a Label's height to fit text

    Because you need a valid graphics object to call the method MeasureString... The MeasureString method accepts a string, and a font, to determine how tall the string will be, which you can use to size your label.

    make sure you pass the labels text, and the labels font when you call it.

  8. #8
    New Member
    Join Date
    Dec 2019
    Posts
    1

    Re: Resize a Label's height to fit text

    Label1.AutoSize = False
    Label1.Width=SomeWidth
    Label1.Caption = "Some text that will appear as multiline text"
    Label1.AutoSize = True

    Damn, did I hunt high and low for this one.

    Outlook VBA 7.1.1091

  9. #9
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Resize a Label's height to fit text

    Hi James,

    I'd try fitting the text in this way:

    Set your Label's text height to Pixels and to your preferred size as x pixels.
    In the following code, make txtHght about (say) half your text height x.
    Code:
            Dim txtHght as Int32 = (½ text height)
            Dim txtSize as Int32 = Label1.Height
            Dim flg as Boonean = Frue
            While flg
                Using gr As Graphics = ctrl.CreateGraphics
                    txtSize = CInt(gr.MeasureString(ctrl.Text, ctrl.Font).Height)
                End Using
                If txtSize > Label1.Height Then
                    Label1.Height += txtHght
                Else
                    flg = False
                End If
            End While

    Poppa.


    Oh !
    I've just noticed this was first posted Sep 1st, 2006 !!
    I guess that mrjjacobs, being a new member, didn't notice that.
    (I wondered how come there were 8,450 Views)

    Pop.
    Last edited by Poppa Mintin; Dec 8th, 2019 at 06:40 PM. Reason: Oh !
    Along with the sunshine there has to be a little rain sometime.

  10. #10
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    57

    Re: Resize a Label's height to fit text

    label.AutoSize = False
    label.Width = CInt("pass your text here")

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