Загрузка данных


using System;
using System.Drawing;
using System.Windows.Forms;

namespace lr16
{
    public class Form1 : Form
    {
        private ComboBox cbA, cbB, cbC;
        private TextBox tbX, tbY, tbZ;
        private RadioButton rbMethod1, rbMethod2;
        private CheckBox cbIncludeZ;
        private Button btnCalculate, btnClear;
        private Label lblResult, lblMethodInfo;
        private ListBox lbHistory;

        public Form1()
        {
            this.Text = "Лабораторная работа №16 — Вариант 10";
            this.Size = new Size(800, 680);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = Color.WhiteSmoke;

            CreateControls();
            LoadDefaultValues();
        }

        private void CreateControls()
        {
            // Заголовок
            var title = new Label
            {
                Text = "Лабораторная работа №16\nМеханизм событий. Простые элементы управления\nВариант 10",
                Font = new Font("Segoe UI", 15, FontStyle.Bold),
                TextAlign = ContentAlignment.MiddleCenter,
                Location = new Point(20, 20),
                Size = new Size(740, 80),
                ForeColor = Color.DarkBlue
            };
            this.Controls.Add(title);

            // Способ расчёта
            var gbMethod = new GroupBox
            {
                Text = "Способ расчёта",
                Location = new Point(30, 110),
                Size = new Size(720, 110)
            };

            lblMethodInfo = new Label
            {
                Text = "Выбран способ: Прямое вычисление",
                Location = new Point(20, 25),
                Size = new Size(500, 20)
            };

            rbMethod1 = new RadioButton
            {
                Text = "Способ 1: Прямое вычисление  W = a·x + b·y + c·z",
                Location = new Point(20, 50),
                Checked = true
            };
            rbMethod1.CheckedChanged += (s, e) => { if (rbMethod1.Checked) lblMethodInfo.Text = "Выбран способ: Прямое вычисление"; };

            rbMethod2 = new RadioButton
            {
                Text = "Способ 2: Через массив коэффициентов и цикл for",
                Location = new Point(20, 75)
            };
            rbMethod2.CheckedChanged += (s, e) => { if (rbMethod2.Checked) lblMethodInfo.Text = "Выбран способ: Через массив и цикл"; };

            gbMethod.Controls.Add(lblMethodInfo);
            gbMethod.Controls.Add(rbMethod1);
            gbMethod.Controls.Add(rbMethod2);
            this.Controls.Add(gbMethod);

            // Коэффициенты
            var gbCoeff = new GroupBox
            {
                Text = "Коэффициенты a, b, c",
                Location = new Point(30, 240),
                Size = new Size(370, 160)
            };

            cbA = new ComboBox { Location = new Point(90, 35), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };
            cbB = new ComboBox { Location = new Point(90, 70), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };
            cbC = new ComboBox { Location = new Point(90, 105), Width = 150, DropDownStyle = ComboBoxStyle.DropDownList };

            gbCoeff.Controls.Add(new Label { Text = "a =", Location = new Point(30, 38) });
            gbCoeff.Controls.Add(new Label { Text = "b =", Location = new Point(30, 73) });
            gbCoeff.Controls.Add(new Label { Text = "c =", Location = new Point(30, 108) });
            gbCoeff.Controls.Add(cbA);
            gbCoeff.Controls.Add(cbB);
            gbCoeff.Controls.Add(cbC);
            this.Controls.Add(gbCoeff);

            // Переменные
            var gbVar = new GroupBox
            {
                Text = "Переменные x, y, z",
                Location = new Point(420, 240),
                Size = new Size(330, 160)
            };

            tbX = new TextBox { Location = new Point(90, 35), Width = 120, Text = "4" };
            tbY = new TextBox { Location = new Point(90, 70), Width = 120, Text = "5" };
            tbZ = new TextBox { Location = new Point(90, 105), Width = 120, Text = "2" };

            cbIncludeZ = new CheckBox
            {
                Text = "Включить z в расчёт",
                Location = new Point(30, 135),
                Checked = true
            };
            cbIncludeZ.CheckedChanged += (s, e) => tbZ.Enabled = cbIncludeZ.Checked;

            gbVar.Controls.Add(new Label { Text = "x =", Location = new Point(30, 38) });
            gbVar.Controls.Add(new Label { Text = "y =", Location = new Point(30, 73) });
            gbVar.Controls.Add(new Label { Text = "z =", Location = new Point(30, 108) });
            gbVar.Controls.Add(tbX);
            gbVar.Controls.Add(tbY);
            gbVar.Controls.Add(tbZ);
            gbVar.Controls.Add(cbIncludeZ);
            this.Controls.Add(gbVar);

            // Кнопки
            btnCalculate = new Button
            {
                Text = "Вычислить W",
                Location = new Point(30, 420),
                Size = new Size(200, 50),
                Font = new Font("Segoe UI", 11, FontStyle.Bold),
                BackColor = Color.FromArgb(0, 122, 204),
                ForeColor = Color.White
            };
            btnCalculate.Click += btnCalculate_Click;

            btnClear = new Button
            {
                Text = "Очистить историю",
                Location = new Point(250, 420),
                Size = new Size(170, 50)
            };
            btnClear.Click += (s, e) => lbHistory.Items.Clear();

            this.Controls.Add(btnCalculate);
            this.Controls.Add(btnClear);

            // Результат
            var gbRes = new GroupBox
            {
                Text = "Результат расчёта",
                Location = new Point(30, 485),
                Size = new Size(720, 80)
            };

            lblResult = new Label
            {
                Text = "Результат: W = ?",
                Location = new Point(20, 30),
                Size = new Size(680, 35),
                Font = new Font("Segoe UI", 14, FontStyle.Bold),
                ForeColor = Color.DarkGreen
            };
            gbRes.Controls.Add(lblResult);
            this.Controls.Add(gbRes);

            // История
            lbHistory = new ListBox
            {
                Location = new Point(30, 580),
                Size = new Size(720, 70),
                Font = new Font("Consolas", 10)
            };
            this.Controls.Add(lbHistory);
        }

        private void LoadDefaultValues()
        {
            string[] values = { "1.0", "1.5", "2.0", "2.5", "3.0" };
            cbA.Items.AddRange(values);
            cbB.Items.AddRange(values);
            cbC.Items.AddRange(values);

            cbA.SelectedIndex = 2;
            cbB.SelectedIndex = 1;
            cbC.SelectedIndex = 3;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbA.SelectedIndex < 0 || cbB.SelectedIndex < 0 || cbC.SelectedIndex < 0)
                {
                    MessageBox.Show("Выберите все коэффициенты a, b, c из списков!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                double a = double.Parse(cbA.Text);
                double b = double.Parse(cbB.Text);
                double c = double.Parse(cbC.Text);
                double x = double.Parse(tbX.Text);
                double y = double.Parse(tbY.Text);
                double z = cbIncludeZ.Checked ? double.Parse(tbZ.Text) : 0;

                double W = 0;
                string method = rbMethod1.Checked ? "Способ 1 (прямой)" : "Способ 2 (цикл)";

                if (rbMethod1.Checked)
                    W = a * x + b * y + c * z;
                else
                {
                    double[] coeffs = { a, b, c };
                    double[] vars = { x, y, z };
                    for (int i = 0; i < 3; i++)
                        W += coeffs[i] * vars[i];
                }

                lbHistory.Items.Add($"{method} → W = {W:F4}");
                lblResult.Text = $"Результат: W = {W:F4}";
                lblResult.ForeColor = Color.DarkGreen;
            }
            catch
            {
                MessageBox.Show("Ошибка ввода данных!\nПроверьте, что все поля заполнены числами.", 
                    "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                lblResult.Text = "Ошибка ввода данных!";
                lblResult.ForeColor = Color.Red;
            }
        }
    }
}