Table of contents
1.
Introduction
2.
Add a label to your Form
3.
Label Properties
4.
Label Events
4.1.
Label Click Event 
4.2.
Label Mouse Enter Event 
5.
FAQs
5.1.
Is C# different from C++?
5.2.
What is C# used for?
5.3.
Why is C# so popular?
5.4.
Which is faster C++ or C#?
5.5.
Is C# or C++ better for games?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C# Label Control

Author Shiva
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Label has text in it and is used to “label” anything (‘duh’). Just like label tag is used in HTML, more or less, it’s the same thing. Just to rephrase it better, label controls are used to give descriptive text for a control. 

You can use a Label to add descriptive text to a TextBox control, for example, to notify the user about the type of input expected in control. Label controls can also be used to provide descriptive text to a Form in order to provide useful information to the user. 

In a WinForms application, the Label Control is perhaps the most commonly used control. The Label control can be used to display text in a specific area on the page. 

In the System.Windows.Forms namespace, the Label class is defined.

It is important to note that the label does not take part in user input or mouse or keyboard events.

Add a label to your Form

Step 1: As demonstrated in the figure below, create a windows form.

 

Step 2: Drag and drop the Label control from the ToolBox onto the Windows form. According to your requirements, a label control can be placed anywhere on the Windows form.

 

Step 3: After dragging and dropping, navigate to the Label control's properties to adjust the Label's parameters to your needs.

 

Label Properties

Some of the most commonly used Label Control properties are listed below. You can change a label's property via the property menu or by adding code in the Form Load.

Name – Is the label's name or identifier. Starting the label name with a short word such as lablFirstName or lablAddress is a recommended approach.

private void Form1_Load(object sender, EventArgs e) { 
  label1.Name = "labl"; 
}

Text – Labels are utilised because of this attribute. This is the text that will appear on the label. For instance,

private void Form1_Load(object sender, EventArgs e) {
  lablName.Text = "Coding Ninjas Studio";
}

Font – A label's font can be changed. 

For example, because the Font property can't be altered once it's been established, we'll make a new font and assign it to the Label's font property.

lablName.Font = new Font("Arial"18);

 

Visible – A boolean property that toggles the Label's visibility. The code below, for example, prevents the label from appearing on the form. This property's default value is true.

lablName.Visible = false;

Label Events

The Click and Mouse Enter Events are two events worth highlighting for the Label control.

Label Click Event 

private void lablName_Click(object sender, EventArgs e) {
  lablName.Text = "Mouse Click Event!";
}

Label Mouse Enter Event 

private void lablName_MouseEnter(object sender, EventArgs e) {
  lblName.Text = "Mouse Enter Event!";
}

Also see,  Ienumerable vs Iqueryable

FAQs

Is C# different from C++?

C++ is an intermediate-level language that extends C with object-oriented capabilities, whereas C# is a high-level language. C++ compiles programs to Machine Codes, whereas C# compiles programs to CLR or Common Language Runtime.

What is C# used for?

C# may be used to construct a variety of programs and applications, including mobile apps, desktop apps, cloud-based services, websites, enterprise software, and games, just like other general-purpose programming languages.

Why is C# so popular?

C# has the advantage of being able to construct applications for both mobile and computer devices. It is also extensively used by the open-source community to create software applications utilizing various frameworks like Mono and the.NET Framework.

Which is faster C++ or C#?

C++ code is substantially faster than C# code, making it a superior choice for applications that require high speed. Your network analysis software, for example, may require C++ code, but performance is unlikely to be an issue for a conventional word processing application written in C#.

Is C# or C++ better for games?

C# and C++ can both be used to make games. C++, on the other hand, has better hardware control on the PC or server. As a result, it's usually a better choice for game creation. Both languages, on the other hand, are for game creation, especially since you won't be making games from scratch (usually).

Also Read About - singleton design pattern in c#  

Conclusion

In this article, we have extensively discussed the Label Control in C#. We hope that this blog has helped you enhance your knowledge regarding the Label Control and if you would like to learn more, check out our articles on Basics of C#Methods in C#, and Type Conversion in C#.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enrol in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

 

Live masterclass