Lab 5: Control Properties and Events
Subject: Advanced Programming
Instructor Ahsan Raees
Student Name
Registration No.
Class
Objectives:
Learn how to use control properties like Font, Color, etc.
Understand how to handle TextBox and Button events.
Practice using multiple controls and validations.
Lab Task 1: Customize Controls and Add Validation
Steps:
1. Open previous project or create a new one.
2. Add Label, TextBox, Button.
3. Set properties (Font, ForeColor, BackColor).
4. On button click, validate input and display name in label.
private void btnDisplay_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtName.Text))
{
MessageBox.Show("Please enter your name.", "Input Required");
txtName.Focus();
}
else
{
lblWelcome.Text = "Welcome, " + txtName.Text + "!";
}
}
Practice Exercises:
1. Enhance login form: validate if fields are empty before displaying message.
2. Add a third TextBox for age and show all info on button click.
3. Make Password field hide characters using PasswordChar.
4. Add CheckBox to decide whether to show MessageBox or not.
5. Use a multiline TextBox and copy its content to a Label on button click.
2. Full Function Calculator:
Create a form with two TextBoxes, four Buttons (Add, Subtract, Multiply, Divide), and one Label.
Sample Code Snippet (Division):
if (double.TryParse(txtNum1.Text, out double num1) &&
double.TryParse(txtNum2.Text, out double num2))
{
if (num2 != 0)
lblResult.Text = "Quotient: " + (num1 / num2).ToString();
else
MessageBox.Show("Cannot divide by zero");
}
else
{
MessageBox.Show("Please enter valid numbers.");
}