C# Windows Forms - Labels


C# Windows Forms : Labels

C# provides us with the Label class that represents a standard Windows Label.

This class obviously belongs to System.Windows.Forms. ```c# namespace System.Windows.Forms


This namespace itself is defined in the `System.Windows.Forms.dll`.
```c#
Assembly System.Windows.Forms.dll

This class derives from the System.Windows.Forms.Control. ```c# public class Label : Control{}

The `Control` is a base class for all the UI controls.

## **Creating a Label**
The `Label` class provides us a contructor that we use to create Labels.

This constructor doesn't take any parameters.
```c#
public Label();

Customizing Labels

The Label class provides us several handy properties that we can use to programmatically influence or customize the label.

Majority of these are overriden from the parent Control class.

Property Return Type Capability Description
Text string get;set; This will get or set the text associated with the label.
AutoEllipsis bool get;set; This will either get or set a value to indicate whether the ellipsis character(...) appears in the Label.
AutoSize bool get; set; Will either get or set a value that indicates whether this Label gets automatically resized to display its contents.
BackgroundImage Image get;set; This will get or set the image to be used as the background of the Label.
BorderStyle BorderStyle get;set; This will get or set the borderstyle of the label. System.Windows.Forms.BorderStyle is an enum comprising of the following: None,FixedSingle ,Fixed3D
Image System.Drawing.Image get;set; This property will get or set an image to be displayed in the Label.
ContentAlignment System.Drawing.ContentAlignment get;set; This will get or set the alignment of an image to be displayed in the control.System.Drawing.ContentAlignmentis an enum.
ImageList ImageList get;set; This will get or set the ImageList that contains the images to display in the Label control.

Label Example

```c# using System; using System.Drawing; using System.Windows.Forms;

namespace Mimi { class Program { [STAThread] static void Main() { Application.Run(new MyForm()); } } class MyForm : Form { public MyForm() { InitialilizeUI(); } private void InitialilizeUI() { Label nameLabel = new Label { AutoSize = true, Location = new Point(300, 200), Text = "Hello World Label Example" }; this.AutoScaleMode = AutoScaleMode.Font; this.ClientSize = new Size(700, 500); this.Controls.Add(nameLabel); this.Text = "Form Exampler"; } } }



RESULT
![C# Windows Forms Labels](/demos/csharp/winforms-labels/demo1.png)

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post