Label
A Label shows text the user cannot edit. It is the simplest control in the toolkit and the one a form uses most: a caption for a text box, a heading, a total at the bottom of a grid, an error under a field. The lesson is short because the control is, and the details are the ones that decide whether a form looks right.
A line of text
using System;
using System.Drawing;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
internal sealed class Form1 : Form
{
public Form1()
{
Text = "Labels";
ClientSize = new Size(420, 200);
var heading = new Label
{
Text = "Invoice 2026-0041",
Font = new Font("Segoe UI", 14F, FontStyle.Bold),
Location = new Point(16, 16),
AutoSize = true,
};
var caption = new Label
{
Text = "&Customer:",
Location = new Point(16, 60),
AutoSize = true,
};
var name = new TextBox { Location = new Point(100, 57), Width = 240 };
var total = new Label
{
Text = "Total: 1 250.00",
Location = new Point(16, 120),
Size = new Size(384, 24),
TextAlign = ContentAlignment.MiddleRight,
BorderStyle = BorderStyle.FixedSingle,
};
Controls.Add(heading);
Controls.Add(caption);
Controls.Add(name);
Controls.Add(total);
}
}
dotnet build app.csproj
Build succeeded.
0 Warning(s)
0 Error(s)
The listing is built and not run: the container has no Windows desktop. What the form does is the documented behaviour of each property. The heading is bold at 14 points and sizes itself to its text; the caption carries an access key, so Alt+C moves the focus to the text box that follows it in the tab order; the total is a fixed-width box, one pixel wide border, with its text against the right edge.
Size and alignment
- AutoSize: on a label the default is true, so the control takes the width its text needs. A caption that changes at run time (a count, a message) grows and shrinks with it. Turn it off only when the size must be fixed, for example to align a column of values.
- TextAlign: a ContentAlignment. MiddleRight is the one a money column uses; MiddleLeft is the default.
- BorderStyle: None (default), FixedSingle or Fixed3D. FixedSingle is a thin frame, which is all a value box needs.
- UseMnemonic: true by default, so the ampersand in &Customer: becomes an access key and the ampersand itself is not drawn. Set it to false to show the character, which is what a label of a file name with an ampersand needs.
- Font: a System.Drawing.Font. The default comes from ApplicationConfiguration.Initialize(); a bold heading makes its own.
LinkLabel
LinkLabel is a label that looks like a link and behaves like a button: it raises LinkClicked and passes the part of the text that was clicked. A caption with two links is one control:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
internal sealed class Form1 : Form
{
public Form1()
{
Text = "Links";
var links = new LinkLabel
{
Text = "Open the folder or read the help",
Location = new Point(16, 16),
AutoSize = true,
};
links.Links.Add(0, 15, "folder");
links.Links.Add(19, 4, "help");
links.LinkClicked += OnLinkClicked;
Controls.Add(links);
}
private void OnLinkClicked(object? sender, LinkLabelLinkClickedEventArgs e)
{
var target = e.Link?.LinkData as string;
if (target == "help")
{
MessageBox.Show("The help page opens here.", "Help");
return;
}
Process.Start(new ProcessStartInfo("explorer.exe", ".") { UseShellExecute = true });
}
}
That listing is a fragment: a form with no Main, so the checker counts it and does not build it. Links.Add(start, length, data) marks a span of the text and attaches any object to it; e.Link.LinkData gives it back in the handler. Process.Start with UseShellExecute = true is how a desktop program opens a folder or a URL in the user's default application.
A label as output
A label is also the cheapest output device of a form: a total at the bottom, a count in a corner, a status. The pattern is always the same, a method that computes the text and a line that assigns it:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
internal sealed class Form1 : Form
{
private readonly Label _total = new()
{
Location = new Point(16, 120),
Size = new Size(384, 24),
TextAlign = ContentAlignment.MiddleRight,
BorderStyle = BorderStyle.FixedSingle,
};
private readonly List<decimal> _amounts = new() { 120.50m, 340.00m, 789.50m };
public Form1()
{
Text = "Total";
ClientSize = new Size(420, 160);
Controls.Add(_total);
ShowTotal();
}
private void ShowTotal()
{
decimal sum = 0m;
foreach (var amount in _amounts)
{
sum += amount;
}
_total.Text = string.Create(
CultureInfo.InvariantCulture,
$"Total: {sum:N2}");
}
}
The formatting is worth a second look: string.Create(CultureInfo.InvariantCulture, ...) formats the number with a dot and two decimals whatever the machine's culture is, which is what a test of the text needs. A user-facing total usually wants the current culture instead, and the lesson about culture in the C# course explains both.
Exercises
- Set AutoSize = false on the heading and describe what happens to the text that no longer fits.
- Give the total a BorderStyle.Fixed3D and compare it with FixedSingle.
- Add a second link to the LinkLabel and handle it in the same handler.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the default value of Label.AutoSize?
Answer
true
A label sizes itself to its text unless AutoSize is turned off.
-
Which TextAlign value puts a total against the right edge?
Answer
ContentAlignment.MiddleRight
TextAlign takes a ContentAlignment, so the vertical position is part of the value.
-
What does the ampersand in a Label's Text do?
Answer
It makes the next character an access key
UseMnemonic is on by default, so Alt plus that character moves the focus to the next control.
-
How do you show an ampersand in a label?
Answer
Set UseMnemonic to false
With UseMnemonic off the text is drawn as written.
-
Which control shows text that looks like a link and raises LinkClicked?
Answer
LinkLabel
LinkLabel marks spans of text with Links.Add and reports which one was clicked.
-
What does Links.Add(start, length, data) attach to a span?
Answer
Any object, returned as e.Link.LinkData
LinkData is a plain object, so it can be a command name, an id or a URL.
-
Which property gives a label a thin frame?
Answer
BorderStyle = BorderStyle.FixedSingle
FixedSingle is the one-pixel frame a value box uses.
-
Why can a Label not replace a TextBox for user input?
Answer
A label cannot take the focus and its text cannot be edited
It is an output control: no caret, no selection and no editing.
-
What does string.Create(CultureInfo.InvariantCulture, ...) change about an interpolated string?
Answer
The number and date formats use the invariant culture
It pins the formatting to one culture, which is what a test of the text needs.
-
Which line of the first listing makes the caption's Alt+C work?
Answer
Text = "&Customer:" with the default UseMnemonic
The ampersand marks the key and sends the focus to the next control in the tab order.
Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows.
Article author: Arthur Isaev