Home » VB.NetRSS

Multiline Labels?

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

Keehun
 

8 Answers Found

 

Answer 1

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!

 

Answer 2

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?"

 

Answer 3

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.

 

Answer 4

thanks,

your soulution is the best one.

 

Answer 5

Yes, thank you.  It is the best.

 

Answer 6

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.

 

Answer 7

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

EndIf

newheight = Me.Height + lblMessage.Height

Me
.Size = New Size(newwidth, newheight)




 

Answer 8

Thanks, GrandpaB. 

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

 
 
Search for  Multiline Labels?
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter