How to build interactive forms using if, else if, and switch
Now that you understand the basics of conditional logic, letβs apply it to real-world WinForms interfaces. These examples show how branching logic can drive validation, navigation, and user feedback based on UI input.
π Example 1: Form Validator β Required Fields
Use branching to check that all required fields are filled out.
private void btnSubmit_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtName.Text))
{
MessageBox.Show("Please enter your name.");
}
else if (string.IsNullOrWhiteSpace(txtEmail.Text))
{
MessageBox.Show("Please enter your email.");
}
else
{
MessageBox.Show("Thanks for submitting your form!");
}
}
β Helps guide users before submitting incomplete data
π Example 2: Login Logic β Role-based Access
Use a ComboBox for roles, and a switch to determine what access is granted:
private void btnLogin_Click(object sender, EventArgs e)
{
string role = comboBoxRole.SelectedItem.ToString();
switch (role)
{
case "Admin":
MessageBox.Show("Welcome, Admin! Full access granted.");
break;
case "Member":
MessageBox.Show("Welcome back, Member. Limited access.");
break;
case "Guest":
MessageBox.Show("Guest access only. Please register.");
break;
default:
MessageBox.Show("Unknown role.");
break;
}
}
β
Use switch when testing for a fixed number of options
π¨ Example 3: Change UI Based on User Choice
Use radio buttons or a dropdown to change the appearance of the form:
private void btnApplyTheme_Click(object sender, EventArgs e)
{
if (radioLight.Checked)
{
this.BackColor = Color.White;
lblTitle.ForeColor = Color.Black;
}
else if (radioDark.Checked)
{
this.BackColor = Color.Black;
lblTitle.ForeColor = Color.White;
}
else if (radioColorful.Checked)
{
this.BackColor = Color.MediumPurple;
lblTitle.ForeColor = Color.Yellow;
}
}
β Dynamic UI updates make the app feel more interactive
π Example 4: Age Checker with Nested Conditions
private void btnCheckAge_Click(object sender, EventArgs e)
{
int age;
if (int.TryParse(txtAge.Text, out age))
{
if (age < 0)
{
lblResult.Text = "Age cannot be negative.";
}
else if (age < 13)
{
lblResult.Text = "You're a child.";
}
else if (age < 18)
{
lblResult.Text = "You're a teenager.";
}
else
{
lblResult.Text = "You're an adult.";
}
}
else
{
lblResult.Text = "Please enter a valid age.";
}
}
β
Uses TryParse() for safe input and multiple condition branches
π Summary of UI Branching Patterns
| Use Case | Logic Type | Example Control |
|---|---|---|
| Field validation | if / else | TextBox, CheckBox |
| Role/user option handling | switch | ComboBox, RadioButton |
| UI customization | if / else if | RadioButton, Button |
| Input type checking | if + TryParse() | TextBox |
β Best Practices
- β Always validate user input before using it
- β
Use
switchwhen options are fixed and known - β
Display user feedback clearly (
Label,MessageBox) - β Keep branching logic inside event handlers or helper methods
π§ͺ Quick Challenge
π§© Build a profile form that:
- Validates name and age
- Checks if the user selected a membership level (
Guest,Member,VIP) - Displays a tailored welcome message in a
Label
Bonus: Change background color for VIPs!
π Want to Go Further?
- Use timers or progress bars to trigger conditional animations
- Store user settings and preferences based on input logic
- Use
enumandswitchtogether for clean, typed logic
π¬ Want help turning your WinForms interface into an interactive experience?
We can help wire up conditional logic for any control, form, or user flow.