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


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

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

        // ====================== ЗАГРУЗКА ФОРМЫ ======================
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] coeffs = { "1.0", "1.5", "2.0", "2.5", "3.0" };

            cbA.Items.AddRange(coeffs);
            cbB.Items.AddRange(coeffs);
            cbC.Items.AddRange(coeffs);

            cbA.SelectedIndex = 2;   // 2.0 по умолчанию
            cbB.SelectedIndex = 1;   // 1.5
            cbC.SelectedIndex = 3;   // 2.5

            tbX.Text = "4";
            tbY.Text = "5";
            tbZ.Text = "2";

            rbMethod1.Checked = true;
        }

        // ====================== КНОПКА ВЫЧИСЛИТЬ ======================
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbA.SelectedIndex == -1 || cbB.SelectedIndex == -1 || cbC.SelectedIndex == -1)
                {
                    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;
            }
        }

        private void rbMethod1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMethod1.Checked)
                lblMethodInfo.Text = "Выбран способ: Прямое вычисление";
        }

        private void rbMethod2_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMethod2.Checked)
                lblMethodInfo.Text = "Выбран способ: Через массив и цикл";
        }

        private void cbIncludeZ_CheckedChanged(object sender, EventArgs e)
        {
            tbZ.Enabled = cbIncludeZ.Checked;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lbHistory.Items.Clear();
        }

        // ====================== DESIGNER КОД ======================
        private System.ComponentModel.IContainer components = null;

        private Label lblTitle;
        private GroupBox gbMethod;
        private Label lblMethodInfo;
        private RadioButton rbMethod1;
        private RadioButton rbMethod2;

        private GroupBox gbCoefficients;
        private Label lblA, lblB, lblC;
        private ComboBox cbA, cbB, cbC;

        private GroupBox gbVariables;
        private Label lblX, lblY, lblZ;
        private TextBox tbX, tbY, tbZ;
        private CheckBox cbIncludeZ;

        private Button btnCalculate;
        private Button btnClear;

        private GroupBox gbResult;
        private Label lblResult;

        private ListBox lbHistory;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();

            // Настройки формы
            this.ClientSize = new Size(780, 670);
            this.Text = "Лабораторная работа №16 — Вариант 10";
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.BackColor = Color.WhiteSmoke;

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

            // Способ расчёта
            gbMethod = new GroupBox
            {
                Text = "Способ расчёта",
                Location = new Point(30, 100),
                Size = new Size(720, 115),
                Font = new Font("Segoe UI", 10F, FontStyle.Bold)
            };

            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), AutoSize = true };
            rbMethod1.CheckedChanged += rbMethod1_CheckedChanged;

            rbMethod2 = new RadioButton { Text = "Способ 2: Через массив коэффициентов и цикл for", Location = new Point(20, 75), AutoSize = true };
            rbMethod2.CheckedChanged += rbMethod2_CheckedChanged;

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

            // Коэффициенты
            gbCoefficients = new GroupBox
            {
                Text = "Коэффициенты a, b, c",
                Location = new Point(30, 225),
                Size = new Size(370, 155),
                Font = new Font("Segoe UI", 10F, FontStyle.Bold)
            };

            lblA = new Label { Text = "a =", Location = new Point(30, 38), AutoSize = true };
            cbA = new ComboBox { Location = new Point(85, 35), Width = 140, DropDownStyle = ComboBoxStyle.DropDownList };

            lblB = new Label { Text = "b =", Location = new Point(30, 73), AutoSize = true };
            cbB = new ComboBox { Location = new Point(85, 70), Width = 140, DropDownStyle = ComboBoxStyle.DropDownList };

            lblC = new Label { Text = "c =", Location = new Point(30, 108), AutoSize = true };
            cbC = new ComboBox { Location = new Point(85, 105), Width = 140, DropDownStyle = ComboBoxStyle.DropDownList };

            gbCoefficients.Controls.Add(lblA); gbCoefficients.Controls.Add(cbA);
            gbCoefficients.Controls.Add(lblB); gbCoefficients.Controls.Add(cbB);
            gbCoefficients.Controls.Add(lblC); gbCoefficients.Controls.Add(cbC);

            // Переменные
            gbVariables = new GroupBox
            {
                Text = "Переменные x, y, z",
                Location = new Point(420, 225),
                Size = new Size(330, 155),
                Font = new Font("Segoe UI", 10F, FontStyle.Bold)
            };

            lblX = new Label { Text = "x =", Location = new Point(30, 38), AutoSize = true };
            tbX = new TextBox { Location = new Point(85, 35), Width = 120, TextAlign = HorizontalAlignment.Center };

            lblY = new Label { Text = "y =", Location = new Point(30, 73), AutoSize = true };
            tbY = new TextBox { Location = new Point(85, 70), Width = 120, TextAlign = HorizontalAlignment.Center };

            lblZ = new Label { Text = "z =", Location = new Point(30, 108), AutoSize = true };
            tbZ = new TextBox { Location = new Point(85, 105), Width = 120, TextAlign = HorizontalAlignment.Center };

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

            gbVariables.Controls.Add(lblX); gbVariables.Controls.Add(tbX);
            gbVariables.Controls.Add(lblY); gbVariables.Controls.Add(tbY);
            gbVariables.Controls.Add(lblZ); gbVariables.Controls.Add(tbZ);
            gbVariables.Controls.Add(cbIncludeZ);

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

            btnClear = new Button
            {
                Text = "Очистить историю",
                Location = new Point(250, 395),
                Size = new Size(170, 50)
            };
            btnClear.Click += btnClear_Click;

            // Результат
            gbResult = new GroupBox
            {
                Text = "Результат расчёта",
                Location = new Point(30, 460),
                Size = new Size(720, 85),
                Font = new Font("Segoe UI", 10F, FontStyle.Bold)
            };

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

            // История
            lbHistory = new ListBox
            {
                Location = new Point(30, 560),
                Size = new Size(720, 90),
                Font = new Font("Consolas", 9.5F)
            };

            // Добавляем элементы на форму
            this.Controls.Add(lblTitle);
            this.Controls.Add(gbMethod);
            this.Controls.Add(gbCoefficients);
            this.Controls.Add(gbVariables);
            this.Controls.Add(btnCalculate);
            this.Controls.Add(btnClear);
            this.Controls.Add(gbResult);
            this.Controls.Add(lbHistory);
        }

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