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.
- Add Labels for "Username:" and "Password:".
- Add TextBoxes next to each label.
- Add a Label that shows status messages.
- 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.