How to display multiple lines on Label in C#

In C#, you can display multiple lines on a Label using the System.Windows.Forms.Label class. You need to set the AutoSize property to false and set the Text property to include line breaks using the newline character \n. Here's how to do it:

  • Create a new Windows Forms Application:

Create a new C# Windows Forms Application using Visual Studio or any other C# development environment. Name it MultilineLabelExample.

  • Add a Label control:

Add a Label control to the form by dragging it from the Toolbox onto the form or by adding the following code inside the Form element in the Form1.Designer.cs file:

private System.Windows.Forms.Label label1;
  • Initialize the Label control:

In the InitializeComponent() method, initialize the Label control and set its properties:

this.label1 = new System.Windows.Forms.Label();

// Set properties
this.label1.AutoSize = false;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Size = new System.Drawing.Size(250, 100);
this.label1.Text = "Line 1\nLine 2\nLine 3";

// Add the label to the form's controls
this.Controls.Add(this.label1);

This code sets the AutoSize property to false and the Text property to include line breaks using the newline character \n. It also sets the location and size of the Label control.

  • Run the application:

Now, run the application. You should see a label displaying three lines of text separated by line breaks:

Line 1
Line 2
Line 3

This example demonstrates how to display multiple lines on a Label in a C# Windows Forms Application by setting the AutoSize property to false and including line breaks in the Text property using the newline character \n.

  1. C# Label Multiple Lines Example:

    Display multiple lines in a Label control in a Windows Forms application.

    // Create a Label
    Label label = new Label();
    
    // Set properties
    label.Text = "Line 1\nLine 2\nLine 3";
    label.Location = new Point(10, 10);
    
    // Add to the form
    this.Controls.Add(label);
    
  2. Displaying Line Breaks in a Label in C#:

    Display line breaks using \n in the text of a Label control.

    label.Text = "Line 1\nLine 2\nLine 3";
    
  3. Multiline Label in Windows Forms C#:

    Enable multiline for a Label in Windows Forms.

    label.Multiline = true;
    
  4. Setting Line Breaks in a Label Control in C#:

    Set line breaks using Environment.NewLine in the text.

    label.Text = "Line 1" + Environment.NewLine + "Line 2" + Environment.NewLine + "Line 3";
    
  5. LineWrap Property in C# Label:

    Use the WordWrap property to enable line wrapping in a Label.

    label.AutoSize = false;
    label.WordWrap = true;
    
  6. Adding New Lines to a Label in C#:

    Add new lines to the text property using \n or Environment.NewLine.

    label.Text = "Line 1\nLine 2\nLine 3";
    
  7. Using Environment.NewLine in a C# Label:

    Utilize Environment.NewLine for consistent newline across platforms.

    label.Text = "Line 1" + Environment.NewLine + "Line 2" + Environment.NewLine + "Line 3";
    

WPF (Windows Presentation Foundation):

  1. WPF Label with Multiple Lines in C#:

    Display multiple lines in a Label in a WPF application.

    <!-- XAML markup -->
    <Label Content="Line 1&#x0a;Line 2&#x0a;Line 3" />
    
  2. String Concatenation for Multiline Label in C#:

    Concatenate strings for multiline labels in a WPF application.

    <Label>
        Line 1
        Line 2
        Line 3
    </Label>
    
  3. C# Label Auto Size Multiline:

    Allow a WPF Label to adjust its size automatically for multiline text.

    <Label Content="Line 1&#x0a;Line 2&#x0a;Line 3" />
    
  4. WordWrap Property in C# Label Control:

    Enable word wrapping for a Label in WPF.

    <Label Content="This is a long text that will wrap to the next line automatically." 
           Width="150" 
           Height="50" 
           ContentStringFormat="Wrap"/>
    
  5. Customizing Text Rendering for Multiline Label in C#:

    Customize text rendering using TextBlock in WPF.

    <TextBlock>
        <Run>This is Line 1</Run>
        <LineBreak/>
        <Run>This is Line 2</Run>
        <LineBreak/>
        <Run>This is Line 3</Run>
    </TextBlock>
    
  6. Line Spacing in C# Label Control:

    Adjust line spacing in a WPF Label using TextBlock.

    <TextBlock LineHeight="20">
        <Run>This is Line 1</Run>
        <LineBreak/>
        <Run>This is Line 2</Run>
        <LineBreak/>
        <Run>This is Line 3</Run>
    </TextBlock>
    
  7. HTML-like Formatting for Labels in C#:

    Achieve HTML-like formatting in WPF using TextBlock and Inline elements.

    <TextBlock>
        <Bold>This is Line 1</Bold>
        <LineBreak/>
        <Italic>This is Line 2</Italic>
        <LineBreak/>
        <Underline>This is Line 3</Underline>
    </TextBlock>
    
  8. Handling Newline Characters in Label Text C#:

    Handle newline characters directly in WPF Label.

    <Label Content="Line 1&#x0a;Line 2&#x0a;Line 3" />