Методичка: Використання CheckBox, RadioButton, ComboBox

Приклад 1: Вибір інгредієнтів


// Масив CheckBox для вибору інгредієнтів
CheckBox[] ingredients = { checkBoxCheese, checkBoxTomato, checkBoxPepperoni };
string selected = string.Join(", ", ingredients.Where(cb => cb.Checked).Select(cb => cb.Text));
MessageBox.Show("Обрано: " + selected);

Приклад 2: Вибір одного варіанту (RadioButton)


string size = radioSmall.Checked ? "Мала" :
              radioMedium.Checked ? "Середня" : "Велика";
MessageBox.Show("Розмір: " + size);

Приклад 3: Вибір із ComboBox


string pizza = comboBoxPizza.SelectedItem.ToString();
MessageBox.Show("Вибрана піца: " + pizza);

Комплексний проект: Замовлення піци

Інгредієнти






Розмір




Тісто



Назва піци

Номер столу (1–200)

Прізвище

C# код комплексного проекту. Form1.Designer.cs (з GroupBox)


namespace PizzaOrderApp
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        private System.Windows.Forms.GroupBox groupIngredients;
        private System.Windows.Forms.CheckBox checkBoxCheese;
        private System.Windows.Forms.CheckBox checkBoxTomato;
        private System.Windows.Forms.CheckBox checkBoxPepperoni;
        private System.Windows.Forms.CheckBox checkBoxOlives;
        private System.Windows.Forms.CheckBox checkBoxMushrooms;

        private System.Windows.Forms.GroupBox groupSize;
        private System.Windows.Forms.RadioButton radioSmall;
        private System.Windows.Forms.RadioButton radioMedium;
        private System.Windows.Forms.RadioButton radioLarge;

        private System.Windows.Forms.GroupBox groupCrust;
        private System.Windows.Forms.RadioButton radioThin;
        private System.Windows.Forms.RadioButton radioThick;

        private System.Windows.Forms.GroupBox groupPizza;
        private System.Windows.Forms.ComboBox comboBoxPizza;

        private System.Windows.Forms.GroupBox groupCustomer;
        private System.Windows.Forms.TextBox textBoxTable;
        private System.Windows.Forms.TextBox textBoxSurname;

        private System.Windows.Forms.Button btnOrder;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null)) components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.groupIngredients = new System.Windows.Forms.GroupBox();
            this.checkBoxCheese = new System.Windows.Forms.CheckBox();
            this.checkBoxTomato = new System.Windows.Forms.CheckBox();
            this.checkBoxPepperoni = new System.Windows.Forms.CheckBox();
            this.checkBoxOlives = new System.Windows.Forms.CheckBox();
            this.checkBoxMushrooms = new System.Windows.Forms.CheckBox();

            this.groupSize = new System.Windows.Forms.GroupBox();
            this.radioSmall = new System.Windows.Forms.RadioButton();
            this.radioMedium = new System.Windows.Forms.RadioButton();
            this.radioLarge = new System.Windows.Forms.RadioButton();

            this.groupCrust = new System.Windows.Forms.GroupBox();
            this.radioThin = new System.Windows.Forms.RadioButton();
            this.radioThick = new System.Windows.Forms.RadioButton();

            this.groupPizza = new System.Windows.Forms.GroupBox();
            this.comboBoxPizza = new System.Windows.Forms.ComboBox();

            this.groupCustomer = new System.Windows.Forms.GroupBox();
            this.textBoxTable = new System.Windows.Forms.TextBox();
            this.textBoxSurname = new System.Windows.Forms.TextBox();

            this.btnOrder = new System.Windows.Forms.Button();

            // 
            // groupIngredients
            // 
            this.groupIngredients.Text = "Інгредієнти";
            this.groupIngredients.Location = new System.Drawing.Point(20, 20);
            this.groupIngredients.Size = new System.Drawing.Size(140, 180);
            this.groupIngredients.Controls.Add(this.checkBoxCheese);
            this.groupIngredients.Controls.Add(this.checkBoxTomato);
            this.groupIngredients.Controls.Add(this.checkBoxPepperoni);
            this.groupIngredients.Controls.Add(this.checkBoxOlives);
            this.groupIngredients.Controls.Add(this.checkBoxMushrooms);

            this.checkBoxCheese.Text = "Сир"; this.checkBoxCheese.Location = new System.Drawing.Point(10, 20);
            this.checkBoxTomato.Text = "Помідори"; this.checkBoxTomato.Location = new System.Drawing.Point(10, 45);
            this.checkBoxPepperoni.Text = "Пепероні"; this.checkBoxPepperoni.Location = new System.Drawing.Point(10, 70);
            this.checkBoxOlives.Text = "Оливки"; this.checkBoxOlives.Location = new System.Drawing.Point(10, 95);
            this.checkBoxMushrooms.Text = "Гриби"; this.checkBoxMushrooms.Location = new System.Drawing.Point(10, 120);

            foreach (var cb in new [] { checkBoxCheese, checkBoxTomato, checkBoxPepperoni, checkBoxOlives, checkBoxMushrooms })
                cb.CheckedChanged += new System.EventHandler(this.InputChanged);

            // 
            // groupSize
            // 
            this.groupSize.Text = "Розмір піци";
            this.groupSize.Location = new System.Drawing.Point(180, 20);
            this.groupSize.Size = new System.Drawing.Size(120, 120);
            this.groupSize.Controls.Add(this.radioSmall);
            this.groupSize.Controls.Add(this.radioMedium);
            this.groupSize.Controls.Add(this.radioLarge);

            this.radioSmall.Text = "Мала"; this.radioSmall.Location = new System.Drawing.Point(10, 20);
            this.radioMedium.Text = "Середня"; this.radioMedium.Location = new System.Drawing.Point(10, 45);
            this.radioLarge.Text = "Велика"; this.radioLarge.Location = new System.Drawing.Point(10, 70);

            foreach (var r in new [] { radioSmall, radioMedium, radioLarge })
                r.CheckedChanged += new System.EventHandler(this.InputChanged);

            // 
            // groupCrust
            // 
            this.groupCrust.Text = "Тісто";
            this.groupCrust.Location = new System.Drawing.Point(180, 150);
            this.groupCrust.Size = new System.Drawing.Size(120, 70);
            this.groupCrust.Controls.Add(this.radioThin);
            this.groupCrust.Controls.Add(this.radioThick);

            this.radioThin.Text = "Тонке"; this.radioThin.Location = new System.Drawing.Point(10, 20);
            this.radioThick.Text = "Товсте"; this.radioThick.Location = new System.Drawing.Point(10, 45);

            foreach (var r in new [] { radioThin, radioThick })
                r.CheckedChanged += new System.EventHandler(this.InputChanged);

            // 
            // groupPizza
            // 
            this.groupPizza.Text = "Назва піци";
            this.groupPizza.Location = new System.Drawing.Point(320, 20);
            this.groupPizza.Size = new System.Drawing.Size(150, 70);
            this.groupPizza.Controls.Add(this.comboBoxPizza);

            this.comboBoxPizza.Items.AddRange(new object[] { "Маргарита", "Пепероні", "Гавайська" });
            this.comboBoxPizza.SelectedIndexChanged += new System.EventHandler(this.InputChanged);

            // 
            // groupCustomer
            // 
            this.groupCustomer.Text = "Клієнт";
            this.groupCustomer.Location = new System.Drawing.Point(320, 110);
            this.groupCustomer.Size = new System.Drawing.Size(180, 100);
            this.groupCustomer.Controls.Add(this.textBoxTable);
            this.groupCustomer.Controls.Add(this.textBoxSurname);

            this.textBoxTable.Width = 150;
            this.textBoxTable.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxTable_KeyPress);
            this.textBoxTable.TextChanged += new System.EventHandler(this.InputChanged);

            this.textBoxSurname.Width = 150;
            this.textBoxSurname.TextChanged += new System.EventHandler(this.InputChanged);

            // 
            // btnOrder
            // 
            this.btnOrder.Text = "Зробити замовлення";
            this.btnOrder.Location = new System.Drawing.Point(320, 220);
            this.btnOrder.Enabled = false;
            this.btnOrder.Click += new System.EventHandler(this.btnOrder_Click);

            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(520, 270);
            this.Controls.Add(this.groupIngredients);
            this.Controls.Add(this.groupSize);
            this.Controls.Add(this.groupCrust);
            this.Controls.Add(this.groupPizza);
            this.Controls.Add(this.groupCustomer);
            this.Controls.Add(this.btnOrder);

            this.Text = "Замовлення піци";
        }
    }
}

// Повний C# код з активацією кнопки лише після заповнення всіх полів
using System;
using System.Linq;
using System.Windows.Forms;

namespace PizzaOrderApp
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        private void InputChanged(object sender, EventArgs e)
        {
            btnOrder.Enabled = (checkBoxCheese.Checked || checkBoxTomato.Checked || checkBoxPepperoni.Checked ||
                                checkBoxOlives.Checked || checkBoxMushrooms.Checked)
                               && (radioSmall.Checked || radioMedium.Checked || radioLarge.Checked)
                               && (radioThin.Checked || radioThick.Checked)
                               && comboBoxPizza.SelectedIndex >= 0
                               && !string.IsNullOrWhiteSpace(textBoxTable.Text)
                               && !string.IsNullOrWhiteSpace(textBoxSurname.Text);
        }

        private void textBoxTable_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            var ingredients = "";
            if (checkBoxCheese.Checked) ingredients += "Сир, ";
            if (checkBoxTomato.Checked) ingredients += "Помідори, ";
            if (checkBoxPepperoni.Checked) ingredients += "Пепероні, ";
            if (checkBoxOlives.Checked) ingredients += "Оливки, ";
            if (checkBoxMushrooms.Checked) ingredients += "Гриби, ";
            ingredients = ingredients.TrimEnd(' ', ',');

            string size = radioSmall.Checked ? "Мала" :
                          radioMedium.Checked ? "Середня" : "Велика";

            string crust = radioThin.Checked ? "Тонке" : "Товсте";

            string pizza = comboBoxPizza.SelectedItem.ToString();
            string table = textBoxTable.Text;
            string surname = textBoxSurname.Text.Trim();

            int price = size == "Мала" ? 100 : size == "Середня" ? 150 : 200;
            price += ingredients.Split(',').Length * 20;

            MessageBox.Show($"Замовлення: {pizza}, {size} пицца, {crust} тісто, " +
                            $"з {ingredients}. Стол: {table}, Прізвище: {surname}. Ціна: {price} грн");
        }
    }
}

Задачи для самостоятельной работы

1. Подсчет фруктов







2. Вибір напою та кольору


3. Заказ компьютера




Процессор




Операционная система


4. Салат на заказ




Размер порции




Заправка


5. Подсчет оценок




Сложность теста




Класс