Placing Hard Returns in a Windows Forms Label
Formatting the contents of a Label.Text
How do I get hard returns in a Windows Label Control so that I can make the text in a Label appear as I want it to? Using Multiple Labels is a real pain.
Many times I have wanted to format the contents of a multi-line label, so that I could have hard returns and indentations in the Label area without having to use multiple Label controls. You can't do this at Design time and I used to create multiple Label controls to get the proper effect.
The simple solution is to add the text to the Label Control at run-time. That way you can place the hard returns and extra spaces (for indentation) anywhere you want them, using only one Label control. Check the following C# code:
| label1.Text = "How to Format A Windows Forms Label, making it multi-Line: \n ";|
label1.Text += " * This is indented line 1\n ";
label1.Text += " * This is indented line 2\n ";
label1.Text += " * A) This is indented line 3\n ";
label1.Text += " * B) This is indented line 4\n ";
You can continue this for as long as you like, making your single Label Control work like a Multi-line text box.
The same code in VB.NET is obviously almost identical.
| label1.Text = "How to Format A Windows Label, making it multi-Line:" & vbCrLf|
label1.Text += " * This is indented line 1 " & vbCrLf
label1.Text += " * This is indented line 2 " & vbCrLf
label1.Text += " * A) This is indented line 3 " & vbCrLf
label1.Text += " * B) This is indented line 4 " & vbCrLf
Sometimes problems in the designer can be solved easily at run-time. This is not a rocket science article, but just a simple Tip or Trick. Hope it helps one person out.