C# Label Control
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:
- 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:
- 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;
- 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 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 !!! :)
C# TextBox Control
Hello everyone, I hope you are doing great. In this article, we will talk about C# TextBox Control. In the previous articles, you have already get familiar with C# Button Control & C# Label Control, this is the sequence of those. In short C# TextBox Control is just same as the other expect some few changing. Before getting started with C# TextBox you must have knowledge of previous concepts. It's not essential but it will give you the edge to understand the advanced elements of C#.
C# TextBox is used to get input from users or it can also be used to display some values to the user. The textbox is a container for text blocks, you can take inputs or show the text as you required in the form of paragraphs. If you used TextBox as the input field then you can only take one line of text as the input but if you want multiple line input then you have to activate the multiple line property of C# TextBox.
You can also change your input field as the password field if you are using the TextBox for password. A user can write input in the textbox and the even user can simply paste the data too in the field. You can also add some advance features like grabbing the copied data automatically when user hover the mouse on the input field, it will be done by accessing the clipboard where the copied data is saved temporarily, but this is the advance feature which we will cover after the basics. So, now let me show you how to use C# TextBox:
C# TextBox Control
- First of all, you have to drag the TextBox from the toolbox and adjust it on your application form as you wanted.
- The Default Name of textbox is 'textbox1' which you can change from the right panel under the property tab.
- You can also change the Text Property inside of the textbox from the property panel, but if you want to change the text runtime or dynamically then you have to code like below.
textBox1.Text = "TheEngineeringProjects.com";
- In the above code, it's clearly mentioned that I have declared the Text property as "TheEngineeringProjects.com".
- Now when you will execute the code, a textbox will appear with the text automatically, as shown in below figure:
- You can also give the value of the textbox with the help of variables, like store the string in any variable and then passed to the textbox. Code is given below:
string var = "TheEngineeringProjects.com";
textBox1.Text = var;
- Till now we have seen How to input value into textbox, now we are gonna have a look at how to extract value from textbox.
- If you want to extract the value from textbox then you have to use below code:
string var;
var = textBox1.Text;
- By using the above code, you can easily store the value of textbox into any variable and make it reusable for the whole application.
- You can set the textbox properties with the help of property panel too.
- If you want to modify the textbox from property panel then the shortcut for that is F4.
- If you want to change the textbox height and width dynamically then you have to use the below code.
- One more thing, you can also change its dimensions from the designer tab by just dragging the coordinates of textbox.
textBox1.Width = 250;
textBox1.Height = 50;
- If you want to change the background color, then below code will help you out:
textBox1.BackColor = Color.Blue;
- If you want to change the foreground color, then here's the code for it:
textBox1.ForeColor = Color.White;
- Now when you apply these codes then you will get something as shown in below figure:
- Now let's have a look at few of its Border styles.
- There are three types of the border which you can apply.
- FixedSingle.
- Fixed3d.
- None.
- If you want to change the border style for textbox then you have to use below codes:
textBox1.BorderStyle = BorderStyle.Fixed3D;
textBox1.BorderStyle = BorderStyle.FixedSingle;
textBox1.BorderStyle = BorderStyle.None;
- Till now we have seen different Properties of C# TextBox, now let's check out few of the events associated with it.
- Few of these events are keydown or keypress events.
- In the below code, I have captured the keystroke from the user and after that I have placed a check for the ENTER keystroke.
- So, whenever you press ENTER in C# TextBox then a Message Box will open up showing "You Pressed Enter".
private void textBox1_keydown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
MessageBox.Show("You Pressed Enter");
}
}
- You can also add some advanced features too like text change event which will occur when user will change the default text.
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
}
- If you want to set the maximum length of the text inside textbox then you can also handle this with max length property like below code.
textBox1.MaxLength = 40;
- Sometimes you have to fix the textbox so that user can't add the value, in short, to make the textbox disable from the input. For this, you have to activate the ReadOnly property as true.
textBox1.ReadOnly = true;
- In the beginning, I have declared that you can make your text box as the multiline textbox for that you have to activate the multiline property as the following code do.
textBox1.Multiline = true;
- If you want your textbox as the password type input field then you have to make the characters as the password using below code.
textBox1.PasswordChar = '*';
- If you want to make your textbox like whenever the user enters any data it will start from the newline then there are two ways to do it, I'm sharing both the codes:
//First Method
textBox1.Text += "your text" + "\r\n";
//Second Method
textBox1.Text += "your text" + Environment.NewLine;
- Sometimes we use textbox only for integer values, but when the user adds an integer value in the textbox it will consider as the string by default, so we have to convert that into the integer. It's the major concept for which beginners are searching. Here in the below code, you will get the basic idea how to retrieve the integer value from the textbox input.
int i;
i = int.Parse (textBox1.Text);
- First, we have taken an integral type variable and then parsed the textbox value through integer. Now, we will parse the textbox with float and double using below code:
//String to Float conversion
float i;
i = float.Parse (textBox1.Text);
//String to Double conversion
double i;
i = float.Parse (textBox1.Text);
- Here we have done with our C# TextBox Control, and elaborate almost every property which we need in daily software development routine.
- If you practice these properties than you will become able to create your own small calculators and inventory systems.
- For just conclusion, I'm going to recap all the properties in one chunk of code given below:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Width = 250;
textBox1.Height = 50;
textBox1.Multiline = true;
textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;
textBox1.BorderStyle = BorderStyle.Fixed3D;
}
private void button1_Click(object sender, EventArgs e)
{
string var;
var = textBox1.Text;
MessageBox.Show(var);
}
}
}
- You need to drag the textbox and button on your form and then copy the code.
- When you will execute this code, it will demand any input from you and when you will enter and press the button it will show you the popup message box in which the input text appears.
- Here's the video which will help you better in understanding C# TextBox control:
So, that's all for today. I hope you have enjoyed today's tutorial on C# TextBox. I will share more tutorials on C# soon. If you have any questions regarding today's tutorial then you can as in comments. Thanks and take care !!! :)