Open In App

Label in C#

Last Updated : 23 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. The Label is a class and it is defined under System.Windows.Forms namespace. In windows form, you can create Label in two different ways:

1. Design-Time: It is the easiest method to create a Label control using the following steps:

  • Step 1: Create a windows form as shown in the below image:
    Visual Studio -> File -> New -> Project -> WindowsFormApp
  • Step 2: Drag the Label control from the ToolBox and drop it on the windows form. You are allowed to place a Label control anywhere on the windows form according to your need.
  • Step 3: After drag and drop you will go to the properties of the Label control to set the properties of the Label according to your need.

    Output:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set create your own Label control using the Label class. Steps to create a dynamic label:

  • Step 1: Create a label using the Label() constructor is provided by the Label class.
    // Creating label using Label class
    Label mylab = new Label();
    
  • Step 2: After creating Label, set the properties of the Label provided by the Label class.
    // Set the text in Label
    mylab.Text = "GeeksforGeeks";
    
    // Set the location of the Label
    mylab.Location = new Point(222, 90);
    
    // Set the AutoSize property of the Label control
    mylab.AutoSize = true;
    
    // Set the font of the content present in the Label Control
    mylab.Font = new Font("Calibri", 18);
    
    // Set the foreground color of the Label control
    mylab.ForeColor = Color.Green;
    
    // Set the padding in the Label control
     mylab.Padding = new Padding(6);
    
  • Step 3: And last add this Label control to form using Add() method.
    // Add this label to the form
    this.Controls.Add(mylab);
    

    Example:




    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp18 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the label
            Label mylab = new Label();
            mylab.Text = "GeeksforGeeks";
            mylab.Location = new Point(222, 90);
            mylab.AutoSize = true;
            mylab.Font = new Font("Calibri", 18);
            mylab.ForeColor = Color.Green;
            mylab.Padding = new Padding(6);
      
            // Adding this control to the form
            this.Controls.Add(mylab);
        }
    }
    }

    
    

    Output:

Important Properties of the Label Control

Property Description
AutoSize This property is used to set a value indicating whether the Label control is automatically resized to display its entire contents.
BackColor This property is used to set the background color for the Label control.
BackgroundImage This property is used to set the background image for the Label control.
BorderStyle This property is used to set the border style for the Label control.
FlatStyle This property is used to set the flat style appearance of the label control.
Font This property is used to set the font of the text displayed by the Label control.
FontHeight This property is used to set the height of the font of the Label control.
ForeColor This property is used to set the foreground color of the Label control.
Height This property is used to set the height of the Label control.
Image This property is used to set the image that is displayed on a Label.
Location This property is used to set the coordinates of the upper-left corner of the Label control relative to the upper-left corner of its form.
Name This property is used to set the name of the Label control.
Padding This property is used to set padding within the Label control.
Size This property is used to set the height and width of the Label control.
Text This property is used to set the text associated with this Label control.
TextAlign This property is used to set the alignment of text in the label.
Visible This property is used to set a value indicating whether the control and all its child controls are displayed.
Width This property is used to set the width of the Label control.


Similar Reads

How to set the Size of the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the size of Label control using Size Property. You can set this property using two different methods: 1. Design-Time: It is the easiest method to set the Size property of the Label contro
2 min read
How to set the Foreground Color of the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the foreground color of the Label control using the ForeColor Property. It makes your label more attractive. It is an ambient property which means if we do not set the value of this prope
3 min read
How to set the Background Color of the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the background color of the Label control using BackColor Property. It makes your label more attractive. You can set this property using two different methods: 1. Design-Time: It is the e
2 min read
How to style the Border of Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to style the border of the Label control using the BorderStyle Property. You can style the border of the label in three different ways and these values are provided by the BorderStyle enum: Fixe
3 min read
How to set the Font of the Content Present in the Label in C#?
In windows forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the font of the content present in the Label control using the Font Property. It makes your label more attractive. You can set this property using two different methods: 1. Design-Time: I
2 min read
How to set Text on the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the text in the Label control using the Text Property. It makes your label more attractive. You can set this property using two different methods: 1. Design-Time: It is the easiest method
3 min read
How to Set the Location of the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the location of the Label control using the Location Property in the windows form. This property contains the coordinates of the upper-left corner of the Label control relative to the upp
3 min read
How to set the Visibility of the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the visibility of the Label control using the Visible Property in the windows form. The label is visible when the value of this property is set to be true. If the value of this property i
3 min read
How to Set the Padding of the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the space between the content and the boundaries of the Label control using the Padding Property in the windows form. You can set this property using two different methods: 1. Design-Time
3 min read
How to set the Alignment of the Text in the Label in C#?
In Windows Forms, Label control is used to display text on the form and it does not take part in user input or in mouse or keyboard events. You are allowed to set the alignment of the text present in the Label control using the TextAlign Property in the windows form. You can set this property using two different methods: 1. Design-Time: It is the e
3 min read
Article Tags :