Загрузка данных
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)
{
// Заполняем ComboBox коэффициентами
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
rbMethod1.Checked = true;
tbX.Text = "4";
tbY.Text = "5";
tbZ.Text = "2";
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
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 = double.Parse(tbZ.Text);
if (!cbIncludeZ.Checked)
z = 0;
double W = 0;
string method = "";
if (rbMethod1.Checked)
{
W = a * x + b * y + c * z;
method = "Способ 1 (прямой)";
}
else
{
double[] coeffs = { a, b, c };
double[] vars = { x, y, z };
for (int i = 0; i < 3; i++)
W += coeffs[i] * vars[i];
method = "Способ 2 (цикл)";
}
string resultStr = $"{method} → W = {W:F4}";
lbHistory.Items.Add(resultStr);
lblResult.Text = $"Результат: W = {W:F4}";
lblResult.ForeColor = Color.DarkGreen;
}
catch
{
MessageBox.Show("Ошибка! Проверьте корректность введённых чисел.",
"Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Error);
lblResult.ForeColor = Color.Red;
lblResult.Text = "Ошибка ввода данных!";
}
}
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)
{
if (MessageBox.Show("Очистить историю расчётов?", "Подтверждение",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
lbHistory.Items.Clear();
}
}
}
}
namespace lr16
{
partial class Form1
{
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 ToolTip toolTip1;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new ToolTip(this.components);
// Основные настройки формы
this.ClientSize = new System.Drawing.Size(740, 630);
this.Text = "Лабораторная работа №16 — Вариант 10";
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.BackColor = System.Drawing.Color.WhiteSmoke;
// Заголовок
this.lblTitle = new Label();
this.lblTitle.Text = "Лабораторная работа №16\nМеханизм событий. Простые элементы управления\nВариант 10";
this.lblTitle.Font = new System.Drawing.Font("Segoe UI", 15F, System.Drawing.FontStyle.Bold);
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lblTitle.Location = new System.Drawing.Point(20, 10);
this.lblTitle.Size = new System.Drawing.Size(700, 80);
this.lblTitle.ForeColor = System.Drawing.Color.DarkBlue;
// Группа выбора метода
this.gbMethod = new GroupBox();
this.gbMethod.Text = "Способ расчёта";
this.gbMethod.Location = new System.Drawing.Point(30, 100);
this.gbMethod.Size = new System.Drawing.Size(680, 110);
this.gbMethod.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold);
this.lblMethodInfo = new Label();
this.lblMethodInfo.Text = "Выбран способ: Прямое вычисление";
this.lblMethodInfo.Location = new System.Drawing.Point(20, 25);
this.lblMethodInfo.Size = new System.Drawing.Size(400, 20);
this.rbMethod1 = new RadioButton();
this.rbMethod1.Text = "Способ 1: Прямое вычисление W = a·x + b·y + c·z";
this.rbMethod1.Location = new System.Drawing.Point(20, 50);
this.rbMethod1.AutoSize = true;
this.rbMethod1.CheckedChanged += new System.EventHandler(this.rbMethod1_CheckedChanged);
this.rbMethod2 = new RadioButton();
this.rbMethod2.Text = "Способ 2: Через массив коэффициентов и цикл for";
this.rbMethod2.Location = new System.Drawing.Point(20, 75);
this.rbMethod2.AutoSize = true;
this.rbMethod2.CheckedChanged += new System.EventHandler(this.rbMethod2_CheckedChanged);
this.gbMethod.Controls.Add(this.lblMethodInfo);
this.gbMethod.Controls.Add(this.rbMethod1);
this.gbMethod.Controls.Add(this.rbMethod2);
// Группа коэффициентов
this.gbCoefficients = new GroupBox();
this.gbCoefficients.Text = "Коэффициенты";
this.gbCoefficients.Location = new System.Drawing.Point(30, 220);
this.gbCoefficients.Size = new System.Drawing.Size(340, 140);
this.gbCoefficients.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold);
this.lblA = new Label() { Text = "a =", Location = new Point(20, 35), AutoSize = true };
this.cbA = new ComboBox() { Location = new Point(70, 32), Size = new Size(110, 25), DropDownStyle = ComboBoxStyle.DropDownList };
this.lblB = new Label() { Text = "b =", Location = new Point(20, 70), AutoSize = true };
this.cbB = new ComboBox() { Location = new Point(70, 67), Size = new Size(110, 25), DropDownStyle = ComboBoxStyle.DropDownList };
this.lblC = new Label() { Text = "c =", Location = new Point(20, 105), AutoSize = true };
this.cbC = new ComboBox() { Location = new Point(70, 102), Size = new Size(110, 25), DropDownStyle = ComboBoxStyle.DropDownList };
this.gbCoefficients.Controls.Add(this.lblA);
this.gbCoefficients.Controls.Add(this.cbA);
this.gbCoefficients.Controls.Add(this.lblB);
this.gbCoefficients.Controls.Add(this.cbB);
this.gbCoefficients.Controls.Add(this.lblC);
this.gbCoefficients.Controls.Add(this.cbC);
// Группа переменных
this.gbVariables = new GroupBox();
this.gbVariables.Text = "Переменные";
this.gbVariables.Location = new System.Drawing.Point(390, 220);
this.gbVariables.Size = new System.Drawing.Size(320, 140);
this.gbVariables.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold);
this.lblX = new Label() { Text = "x =", Location = new Point(20, 35), AutoSize = true };
this.tbX = new TextBox() { Location = new Point(70, 32), Size = new Size(110, 25), TextAlign = HorizontalAlignment.Center };
this.lblY = new Label() { Text = "y =", Location = new Point(20, 70), AutoSize = true };
this.tbY = new TextBox() { Location = new Point(70, 67), Size = new Size(110, 25), TextAlign = HorizontalAlignment.Center };
this.lblZ = new Label() { Text = "z =", Location = new Point(20, 105), AutoSize = true };
this.tbZ = new TextBox() { Location = new Point(70, 102), Size = new Size(110, 25), TextAlign = HorizontalAlignment.Center };
this.cbIncludeZ = new CheckBox();
this.cbIncludeZ.Text = "Включить z в расчёт";
this.cbIncludeZ.Location = new Point(20, 135);
this.cbIncludeZ.Checked = true;
this.cbIncludeZ.CheckedChanged += new System.EventHandler(this.cbIncludeZ_CheckedChanged);
this.gbVariables.Controls.Add(this.lblX);
this.gbVariables.Controls.Add(this.tbX);
this.gbVariables.Controls.Add(this.lblY);
this.gbVariables.Controls.Add(this.tbY);
this.gbVariables.Controls.Add(this.lblZ);
this.gbVariables.Controls.Add(this.tbZ);
this.gbVariables.Controls.Add(this.cbIncludeZ);
// Кнопки
this.btnCalculate = new Button();
this.btnCalculate.Text = "Вычислить";
this.btnCalculate.Location = new System.Drawing.Point(30, 380);
this.btnCalculate.Size = new System.Drawing.Size(180, 50);
this.btnCalculate.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Bold);
this.btnCalculate.BackColor = System.Drawing.Color.FromArgb(0, 122, 204);
this.btnCalculate.ForeColor = System.Drawing.Color.White;
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
this.btnClear = new Button();
this.btnClear.Text = "Очистить историю";
this.btnClear.Location = new System.Drawing.Point(230, 380);
this.btnClear.Size = new System.Drawing.Size(150, 50);
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
// Группа результата
this.gbResult = new GroupBox();
this.gbResult.Text = "Результат";
this.gbResult.Location = new System.Drawing.Point(30, 445);
this.gbResult.Size = new System.Drawing.Size(680, 80);
this.gbResult.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold);
this.lblResult = new Label();
this.lblResult.Text = "Результат: W = ?";
this.lblResult.Location = new System.Drawing.Point(20, 30);
this.lblResult.Size = new System.Drawing.Size(640, 35);
this.lblResult.Font = new System.Drawing.Font("Segoe UI", 14F, System.Drawing.FontStyle.Bold);
this.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.gbResult.Controls.Add(this.lblResult);
// История расчётов
this.lbHistory = new ListBox();
this.lbHistory.Location = new System.Drawing.Point(30, 540);
this.lbHistory.Size = new System.Drawing.Size(680, 75);
this.lbHistory.Font = new System.Drawing.Font("Consolas", 9.5F);
// Добавляем все элементы на форму
this.Controls.Add(this.lblTitle);
this.Controls.Add(this.gbMethod);
this.Controls.Add(this.gbCoefficients);
this.Controls.Add(this.gbVariables);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.gbResult);
this.Controls.Add(this.lbHistory);
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}