TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Label Example: Windows Forms

Label. One useful control in Windows Forms is the Label control. This control serves as an invisible frame where you can place text. Labels can have lots of text, but often only have a few words. They can be mutated in your C# code.
Start. As with other Windows Forms controls, the Label control should be added by opening the Toolbox pane and dragging or double-clicking the Label item. Initially, the label will have the Text property set as "label1".

Note: Obviously, this is not what you will want it to read in your finished application.

Please right-click on the label and select Properties. Then scroll down to Text and you can change what the label reads. The Text property can also be set in the C# code. This can be done in an event.
Font, color. Changing the color and font of a Label is sometimes necessary. But often it may actually detract from the appearance. 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.

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.

Click. Labels are not simply static controls in Windows Forms. They can be mutated through the event handler system as with other controls. 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.

C# program that uses Click event handler on Label 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(); } } }
Summary. The Label control in the Windows Forms toolkit is the ideal container for small text fragments and, fittingly, for labeling other controls in your window. But the label need not be static.

And: Through the event model, you can mutate the Text property, the coloring, and other properties.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf