Home
C#.WinForms
Label Example
Updated Sep 28, 2022
Dot Net Perls
Label. One useful control in Windows Forms is the Label control. This control serves as an invisible frame where you can place text.
Control info. Labels can have lots of text, but often only have a few words. They can be mutated in your C# code using the Click event handler.
Click. Labels can be mutated through the event handler system. You can, for example, change the Text property whenever the user clicks on the label control.
Tip To add the Click event handler, please open Properties and click on the lightning bolt. Then, double-click on the Click item.
using System; using System.Windows.Forms; namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { // Change the text of the label to a random file name. // ... Occurs when you click on the label. label1.Text = System.IO.Path.GetRandomFileName(); } } }
Font, color. We can change the color and font of a Label. To change the color of the text, open the Properties window for the label and scroll to ForeColor.
Then Select a color. It would be best to use a system color. The user could have adjusted the theme colors in some way.
Note In the screenshot, the ForeColor was set to Red. This could help indicate a warning.
Font. It is also possible to change the font and font size. These options are available when you select Properties and then Font, and then expand the Font item with the plus sign.
Font
Tip You can change many of the individual options for the Font on the label. You can, for example, set that the font be bold here.
A summary. The Label control in the Windows Forms toolkit is the ideal container for small text fragments in your window. The appearance of Labels can be adjusted in many ways.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 28, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen