Beginner 5 min read Control #9

Label Control

The Label is one of the simplest and most used controls. It displays text that users can't edit.

What is a Label?

Labels display static text. They're used to describe other controls, show information, or act as headers. Users cannot edit label text directly.

Common uses: Field names (First Name:), headers, status messages, instructions.

Code Example

// Set label text
lblName.Text = "Enter your name:";

// Change label text dynamically
lblStatus.Text = "Loading...";
lblStatus.ForeColor = Color.Blue;

// Update label after operation
private void btnSave_Click(object sender, EventArgs e)
{
    // ... save logic ...
    lblStatus.Text = "Saved successfully!";
    lblStatus.ForeColor = Color.Green;
}

// Use mnemonic (Alt + key to focus)
lblName.Text = "&Name:";  // Alt+N focuses the TextBox
lblName.UseMnemonic = true;

Exercise

Task: Create a simple login form using Labels.

  1. Add Labels for "Username:" and "Password:".
  2. Add TextBoxes next to each label.
  3. Add a Label that shows status messages.
  4. When login button is clicked, update the status label.
Key Takeaway

Labels are essential for providing context to users. Use them to describe other controls and show status information.