Open In App

Label in C#

Last Updated : 20 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Windows Forms, Label control is used to display text on the form. It does not interact with user input or handle mouse or keyboard events. Labels are used to provide information to the user within the form such as description, message, or details. These are the key points about labels.

  • Display Text or Image: It is mainly used to show the text or image in form.
  • Non-Interactive: It is non-interactive and just shows the text not like buttons and textbox.
  • Positioning: Labels can be placed anywhere on the form we can use drag and drop and also specify their coordinates using code.
  • Auto-Size: Labels can automatically adjust their size and fit according to the content when we set the AutoSize property as true.

Ways To Create Labels in Windows Forms

There are mainly two ways to create labels in the Windows Forms:

  • Design Time (Drag and drop)
  • Run Time (Custom code)

Design Time ( Drag and drop)

This is the easiest way to create labels in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the label on the form in the designer and further we can change the appearance of the label using the properties. Follow these steps to create a label.

Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

Empth-forms

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.


Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

ToolBox


Step 3: Choose labels from the common controls in Toolbox as shown below:

LabelIntro


And then drag-and-drop in the form:

DragAndDrop


Step 4. Now open the properties of the label, press right-click on the label and go to properties it will open Solution Explorer now we can change the appearance and behaviour of the label in properties.

Properties


Now we can change the appearance and behaviour of the label such as background and text color or font size. These are the changes we made to the label

PropertiesAfterChange


Similarly, we can create different labels here is the output

Output:

Output


Run Time (Custom Code)

In this method, we are going to modify the Form1.cs file and add custom code modification in C# to change the appearance of the button according to our requirements. Follow these step-by-step processes.

Step 1: Create a label using the Label() constructor provided by the Label class.

// Creating label using Label class

Label mylab = new Label();


Step 2: After creating the 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 the Add() method.

// Add this label to the form

this.Controls.Add(mylab);


Step 4: Now double-click on the form in Design and it will open the Form.cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file

Form1.cs file:

C#
namespace WinFormsApp1
{
    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:

Output


Properties of Label Control

PropertyDescription
AutoSizeThis property is used to set a value indicating whether the Label control is automatically resized to display its entire contents.
BackColorThis property is used to set the background colour for the Label control.
BackgroundImageThis property is used to set the background image for the Label control.
BorderStyleThis property is used to set the border style for the Label control.
FlatStyleThis property is used to set the flat style appearance of the label control.
FontThis property is used to set the font of the text displayed by the Label control.
FontHeightThis property is used to set the height of the font of the Label control.
ForeColorThis property is used to set the foreground colour of the Label control.
HeightThis property is used to set the height of the Label control.
ImageThis property is used to set the image that is displayed on a Label.
LocationThis 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.
NameThis property is used to set the name of the Label control.
PaddingThis property is used to set padding within the Label control.
SizeThis property is used to set the height and width of the Label control.
TextThis property is used to set the text associated with this Label control.
TextAlignThis property is used to set the alignment of text in the label.
VisibleThis property is used to set a value indicating whether the control and all its child controls are displayed.
WidthThis property is used to set the width of the Label control.

Next Article
Article Tags :

Similar Reads