Here we focus on conditional logic in C# WinForms using if, else if, else, and switch. These control structures help your app make decisions based on user input and application state.
Using if, else if, and switch to make decisions in your WinForms apps
In real-world apps, you’ll often need to ask:
“What did the user do?”
“Which option did they select?”
“Should I show an error, or a success message?”
That’s where conditional logic comes in.
🧰 Why Use Conditional Logic?
Conditional logic lets your app:
- Respond to user choices
- Validate input and show helpful messages
- Perform different actions based on roles, modes, or settings
🚦 if, else if, and else – The Basics
if (condition)
{
// Run this if condition is true
}
else if (anotherCondition)
{
// Run this if second condition is true
}
else
{
// Run this if none are true
}
✅ Use when testing true/false expressions like numbers, strings, checkbox values, etc.
🧱 Example: Age Checker
private void btnCheckAge_Click(object sender, EventArgs e)
{
int age;
if (int.TryParse(txtAge.Text, out age))
{
if (age < 13)
{
lblMessage.Text = "You're a child.";
}
else if (age < 20)
{
lblMessage.Text = "You're a teenager.";
}
else
{
lblMessage.Text = "You're an adult.";
}
}
else
{
lblMessage.Text = "Please enter a valid number.";
}
}
🔄 switch – For Known Value Sets
Use switch when checking one variable against many fixed values, like enum options or menu selections.
switch (comboBoxRole.SelectedItem.ToString())
{
case "Guest":
lblAccess.Text = "Limited access.";
break;
case "Member":
lblAccess.Text = "Standard access.";
break;
case "Admin":
lblAccess.Text = "Full access granted!";
break;
default:
lblAccess.Text = "Unknown role.";
break;
}
💡 Works great with enum types too!
🎯 Example: Using enum and switch
enum UserType
{
Guest,
Member,
Admin
}
private void ShowAccess(UserType user)
{
switch (user)
{
case UserType.Guest:
MessageBox.Show("Read-only access");
break;
case UserType.Member:
MessageBox.Show("Can edit and comment");
break;
case UserType.Admin:
MessageBox.Show("Full permissions");
break;
}
}
Then call:
ShowAccess(UserType.Admin);
🧪 Quick Challenge
🧩 Create a form with:
- A
TextBoxfor age - A
Buttonto check age group - A
Labelto show the result
Use:
TryParse()to read the inputif / else ifto display child, teen, adult
Bonus: Add a ComboBox for “Role” (Guest, Member, Admin)
Use switch to update permissions in another Label.
📚 Summary
| Keyword | Use When… |
|---|---|
if | You want to test a condition (true/false) |
else if | You want multiple possible branches |
else | You need a fallback when none match |
switch | You’re checking a value with known options |
✅ Best Practices
- ✅ Use
TryParse()to avoid crashy conversions - ✅ Don’t nest too deeply — split into helper methods if needed
- ✅ Keep comparisons simple (break up long conditions)
- ✅ Use
switchwith enums or fixed option lists
🎓 Want to Go Further?
- Use
&&and||to combine conditions:if (age > 18 && isMember) - Use
switch expressionsin C# 8+ for compact logic - Move logic into reusable methods or classes
💬 Not sure if you should use if or switch for a UI decision?
I can help simplify your control flow and tidy your form code.