Results 1 to 4 of 4

Thread: What is an efficient way to resize a label so its smaller than the parent container?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    Question What is an efficient way to resize a label so its smaller than the parent container?

    I'm looking for a way to insert a label into a flowlayoutpanel without it having to scroll horizontally. Therefore, autosize is out of the question. I have it right now so it doesn't exceed the flowlayoutpanel using the code:

    Code:
    newlabel.Width = Parent.Width - (newlabel.Width / 2)
    But I still can't figure out how to size it vertically. I need to make it so the label is only slightly longer than the last line of text. What is the most efficient way to try to accomplish this?

  2. #2
    Member
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    58

    Re: What is an efficient way to resize a label so its smaller than the parent contain

    This will give you the drawn size of the label - you should be able to use it to check the bounds of the label (and act accordingly):
    vb Code:
    1. Dim szF As SizeF = Me.Label1.CreateGraphics.MeasureString(Me.Label1.Text, Me.Label1.Font)
    2. MsgBox(szF.Width.ToString & " " & szF.Height.ToString)

  3. #3
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: What is an efficient way to resize a label so its smaller than the parent contain

    Quote Originally Posted by Chris147 View Post
    This will give you the drawn size of the label - you should be able to use it to check the bounds of the label (and act accordingly):
    That's nearly right but it's a multiline label. All you need to do is add the target width of the label as the next argument:
    Code:
    Dim szF As SizeF = Me.Label1.CreateGraphics.MeasureString _
    (Me.Label1.Text, Me.Label1.Font, Label1.Width)
    Then it will allow for word-wrapping of the text.

    BB

  4. #4
    Member
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    58

    Re: What is an efficient way to resize a label so its smaller than the parent contain

    You see, that's why I like tech forums - thanks BB, I learned something today.

    Chris.

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