Home » C# Basics » 08 - Windows Programming - 1
8

Label Control

Defines the properties of the label control in a windows application

Label control is used to display informative text on the form. For example, we can place a label control beside a textbox to inform the user what she is required to enter in the textbox.

The common properties of the Label control are:

(Name)

The name used to access the control in code.

FlatStyle

To change the style of the label.

Image

To specify an image to be displayed on the label.

Text

The caption of the label.

Visible

To decide if the label is visible or not.

Font

To set the style of the font of the label text.

ForeColor

To set the color of the label text.

BackColor

To set the background color of the label.

BorderStyle

To set the border style.

Before moving on, let us create a simple application to understand how windows programming work, using the controls we have learned so far:

1. Start a new Windows Application project in the Visual C# IDE. Drag two label controls, one textbox control and one button control on the form and arrange them as shown:

2. Next, set the properties of the form and controls as shown in the table:

Property

Value

Form

Text

StartPosition

MaximizeBox

FormBorderStyle

First Application

Center Screen

False

FixedSingle

label1

Text

Enter Your Name

textBox1

(Name)

txtName

label2

(Name)

Text

ForeColor

lblMessage

Blank (delete the text `label2 in the properties window)

Blue

button1

(Name)

Text

btnClick

&Click

The form should look like this:

3. Double click on the button control. This will open up the code view in a separate tab. Note that the typing cursor is placed inside the click event handler of the button. Type the following code inside the braces of the event handler:

private void btnClick_Click(object sender, EventArgs e) { string userName;
userName = txtName.Text;
lblMessage.Text = "Welcome" + userName; }

In the first statement, we have declared a variable `userName' of string type. The next statement assigns the text entered in the textbox to this variable. The text entered in the textbox is accessed using the text property. Hence, txtName.text refers to the text in the textbox named txtName.

The third statement creates a string by appending the `userName' variable to the message `Welcome' and then assigns this complete string to the text property of the label lblMessage.

Remember that this code will execute when the user clicks on the button control.

4. Save the project and then run it (by pressing Ctrl+F5). Enter a name in the textbox and then click the button (or press Alt+C). Here is a sample run:

Note that if the user clicks the button without entering a name in the textbox, only the message `Welcome' will be displayed. If we want to avoid this situation, we need to modify the code as follows:

private void btnClick_Click(object sender, EventArgs e) { string userName;
if (txtName.Text.Length == 0) { MessageBox.Show("Please enter a name"); txtName.Focus(); } else {
userName = txtName.Text;
lblMessage.Text = "Welcome " + userName; } }

Here, we are checking a condition using the `if' statement. txtName.Text.Length will return the length of the text entered in the textbox txtName. If this length is equal to zero i.e. there is no text in the textbox, a dialog box will be displayed with the message `Please enter a name'. The MessageBox.Show() method displays a dialog box with message specified by the developer.

After displaying the message, the txtName.Focus() method will place the typing cursor again inside the textbox so that the user can enter text.

If, however, the user enters a text before clicking the button, the condition becomes false. In this case, the `else' part will be executed and the application will work normally.