locked
Multiline Labels? RRS feed

  • Question

  • Hello All,
    I've been trying to make multiline labels.
    That's not possible through code?
    That dissapoints me.

    Keehun
    Thursday, August 3, 2006 2:14 PM

Answers

  • Unlike a TextBox, to make a Label display a multiline string you do not have to change anything... all you need to do is make sure that you’ve got new line characters in you string which could be as simple as:

            'The more VB style way
            Label1.Text = "Hello" + vbCrLf + "How are you?"
    or
            'The works in almost any (.NET) language way
            Label1.Text = "Hello" + Environment.NewLine + "How are you?"

    Thursday, August 3, 2006 2:33 PM

All replies

  • Unlike a TextBox, to make a Label display a multiline string you do not have to change anything... all you need to do is make sure that you’ve got new line characters in you string which could be as simple as:

            'The more VB style way
            Label1.Text = "Hello" + vbCrLf + "How are you?"
    or
            'The works in almost any (.NET) language way
            Label1.Text = "Hello" + Environment.NewLine + "How are you?"

    Thursday, August 3, 2006 2:33 PM
  • Knvb1123,

     

    Here is another suggestion on how to achieve multiline labels.  Instead of sprinkling the text with vbCrLf characters, I found that if I set the label's MaximumSize property to the size of the label, the text will line-wrap within the label.  The AutoSize property must also be set to True.  I found that it is easier than playing with where to insert the vbCrLf characters.  Best of luck.

    • Proposed as answer by Pitiko2 Wednesday, January 14, 2009 4:47 AM
    Sunday, October 15, 2006 9:44 PM
  • thanks,

    your soulution is the best one.

     

    Thursday, March 27, 2008 1:04 PM
  • Yes, thank you.  It is the best.

     

    Thursday, August 28, 2008 2:58 PM
  • If your problem is solved, you should click the "Mark as Answer" button on the response that helped you.  When other programmers are looking for a solution they'll know to click on your thread.

     

    • Proposed as answer by keef_riff_hard Thursday, August 20, 2009 6:09 PM
    Tuesday, September 30, 2008 7:40 PM
  • Just to throw in a solution that I did that uses multiline labels

    I wanted to create a replacement for MessageBox.Show for errors that had extra features such as "save to file"

    In VS 2005 (VB.NET) I created a new dialog form

    I added a single label and I added/replaced the actions in the TableLayoutPanel

    To make the label act like messagebox text and the form grow like messagebox I did the following

    label properties
    autosize = true
    maximumsize = some reasonable size

    The result is the label will grow in height AND it will word wrap based on maximum width

    Then for the form, on form_load I put logic to calculate the new height and width and did a size

    For the width I used some logic on found on the web (I'm not sure this is the best way but it seemed to make sense) - for the height I simply added the height of the label to the dialog height

    Here is some psudeo code...

    Dim

     

    textFont As Font = lblMessage.Font

     

    'Specify a fixed width, but let the height be "unlimited"

     

    Dim layoutSize As SizeF = New SizeF(lblMessage.Width, 5000.0F)

     

    Dim g As Graphics = Graphics.FromHwnd(lblMessage.Handle)

     

    Dim stringSize As SizeF = g.MeasureString(Text, textFont, layoutSize)

    g.Dispose()

    If

     

    (layoutSize.Width + iSomeAdditionalSpace) > iCurrentDialogWidth Then

        newwidth = layoutSize.Width +

    Me.PictureBox1.Width + iSomeAdditionalSpace

     

    End If

     

     

    newheight = Me.Height + lblMessage.Height

    Me
    .Size = New Size(newwidth, newheight)




    • Proposed as answer by keef_riff_hard Thursday, August 20, 2009 6:23 PM
    Thursday, August 20, 2009 6:22 PM
  • Thanks, GrandpaB. 

    This may be an old post, but it was helpful.

    Wednesday, November 3, 2010 7:14 PM
  • Hey the two ways that i know work and seem to be a bit more simple:

     

    1(via code). just use lableName.Text = "this is first line\nthis is second line";

    or

     

    2(in the properties box). in Visual Studios click on the little arrow next to the Text property, then hit enter and it returns a new line, this only works when you have clicked the arrow the and a white box pops up otherwise it reads the enter as accept.

     

    Hope this helps!

    Tuesday, January 25, 2011 6:48 PM