C# label,Csharp label,C sharp label, label c#,label csharp,label c sharp

C# label,Csharp label,C sharp label, label c#,label csharp,label c sharp
Hello everyone, I hope you all are doing great. In today's article, I will guide you about the C# Label Control. I will show you how to deal with label in C Sharp Programming Language. Labels have their own importance in the software development to nominate the input and output fields. You have observed that every software which you have used and with you are familiar have some labels. Such as in the website login forms there are labels too. It's recommended to use the labels in the software to make the user interface rich with the helping material. If you will just use the C# Button, C# TextBox, and C# ComboBox and don't use the C# Labels then your developed software will not be easy to use. In simple words, it won't be user-friendly. So, in order to make it user-friendly, we have to use Labels in our software. So, let's have a look at How to use C# Label Control and How to change its Properties:

C# Label Control

  • We used the C# Label to display the text on the software to give direction for the user.
  • Label control is also used to show the descriptive text such as the notes and warning about the usage of the software.
  • To define the label in C# you have to declare namespace as follows.
System.Windows.Forms

  • You can also add the label from the toolbox and drag the label from the form designer tab.
  • Label1 is the default Name of the very first label which you will use in your application.
  • You can adjust the size on the form by stretch the coordinates.
  • You can change the text from the right panel under the property label.
  • You can also change the text using the below code:
label1.Text = "TEP C# Label Control Example Text";
  • Here's the output of this code:
C# label,Csharp label,C sharp label, label c#,label csharp,label c sharp
  • You can do anything with the label box. There are many properties which you can use to manipulate the functionality of simple label.
  • For example, if you want to change the background color of the label then you need to use BackColor property.
  • BackColor Property is used to change the background color, after the equal (=) sign you have to declare that you want to change color by writing the property Color and after dot, you have to write the name of the color which you want to allocate to the background
  • In the below code I have declared the Aqua color for the background, there are many colors which you can be used for the background of the label.
label1.BackColor = Color.Aqua;
  • Here's the output of above code:
C# label,Csharp label,C sharp label, label c#,label csharp,label c sharp
  • If you want to change the foreground color or the color of text, then you need to use ForeColor Property.
  • ForeColor is the name of the property which is used to assign the color of the text in the label.
  • Here's the code for it:
label1.ForeColor = Color.BlueViolet;

C# label,Csharp label,C sharp label, label c#,label csharp,label c sharp

  • If you want to set the images as the background of C# label then need to use below code:
label1.Image = Image.FromFile("C:\\Users\\Jade\\Pictures\\brownImage.jpg");
  • Image property is used to set the image. You have to use the Image.FormFile method to set the proper path of an image which you want to set.
  • In double quotation write the path and used "\\" to separate the directories, you have to mention the image with the extension. It will generate the following results.

C# label

C# Label Events

Let's do some advanced code to improve the interaction level of your application. Now let's study the builtin events which we can apply on our C# Label to improve its functionality. Events are created to perform some functionalities when user will do any specific task. There are several Events which you can use to implement on your label, here are the few of them:
  • Click Event
  • Double Click Event
  • Text Changed Event
  • MouseHover Event
  • MouseLeave Event

There are several more events which we can use, but the main task here is to just give you the basic concept of C# Label. That's why we will focus on most commonly used events.

C# Label Click Event

  • The first event is the Click Event, which will perform when user will click on the label.
  • Let me give you an example, suppose you need to create a program in which you want to surprise someone, so now when someone click on your software then a hidden message shows up. It could be Happy Birthday !! ;)
  • Here's the code for it, you can change the text to anything you want.
private void label1_Click(object sender, EventArgs e)
        {
            label1.Text = "Text Changed";
        }
  • After the default name, you have to write the _Click  and create the method and declare the functionality which you want to perform.
  • You can do any kind of manipulation inside click event.

C# Label Double Click Event

  • This event will occur when user will click twice on the label.
  • Here's the code for it:
 private void label1_DoubleClick(object sender, EventArgs e)
        {
            label1.Text = "Text Changed";
        }
  • After the default name, you have to write _DoubleClick method and declare the functionality which you want to perform. It will occur when user will click twice on the label.

C# Label Text Changed Event

  • This event will occur when the text of the label is changed.
  • Suppose you want to change any value on the application when the text of the label is changed. Then you will use the TextChanged Event.
  • Let me explain it with an example, I have taken two labels.
  • Now the logic is when the text of one label is changed then the other label text should change automatically.
  • For the first label, I will use the _Click Event and _TextChanged Event too.
  • So, now when user will click on the label, the text of label will change because of _Click Event
  • Now, when text is changed, _TextChanged Event will occur and 2nd label text will also change.
  • Here's the code which you should try.
private void label1_Click(object sender, EventArgs e)
        {
            label1.Text = "Text Changed";
        }

        

        private void label1_TextChanged(object sender, EventArgs e)
        {
            label2.Text = "2nd Text Changed";

        }
  • To test the above code you have to drag two labels on your application. Then copy the above code into your application.

C# Label MouseHover Event

  • This event will occur when user will hover the mouse cursor on the label.
  • Suppose you want to change the text when user will hover the mouse cursor on the label.
  • Here's the code for it:
private void label1_MouseHover(object sender, EventArgs e)
        {
            label1.Text = "Text Changed";
        }

C# Label MouseLeave Event

  • This event will occur when the cursor will leave the label.
  • Suppose you want to change the text of label when mouse cursor will leave the label.
  • Here is the code, which will perform this functionality:
private void label1_MouseLeave(object sender, EventArgs e)
        {
            label1.Text = "again Text Changed";
        }
  • By the combination of the above events and methods, you can create your own C# applications with multiple functionalities.

If you are looking to learn more about the C# label control and other features of the C# programming language than you should subscribe our YouTube channel where we have uploaded latest tutorials on C#. Thanks for reading, have a good day !!! :)