Practical UI-Driven Examples for Branching Logic in C# WinForms

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 CaseLogic TypeExample Control
Field validationif / elseTextBox, CheckBox
Role/user option handlingswitchComboBox, RadioButton
UI customizationif / else ifRadioButton, Button
Input type checkingif + TryParse()TextBox

βœ… Best Practices

  • βœ… Always validate user input before using it
  • βœ… Use switch when 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 enum and switch together 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.