Загрузка данных
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
{
// Проверка, выбраны ли коэффициенты
if (cbA.SelectedIndex == -1 || cbB.SelectedIndex == -1 || cbC.SelectedIndex == -1)
{
MessageBox.Show("Пожалуйста, выберите все коэффициенты a, b и c из выпадающих списков!",
"Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Проверка заполненности полей переменных
if (string.IsNullOrWhiteSpace(tbX.Text) ||
string.IsNullOrWhiteSpace(tbY.Text) ||
string.IsNullOrWhiteSpace(tbZ.Text))
{
MessageBox.Show("Заполните все поля переменных x, y, z!",
"Внимание", 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 = double.Parse(tbZ.Text);
// Если флажок снят — обнуляем z
if (!cbIncludeZ.Checked)
z = 0;
double W = 0;
string method = rbMethod1.Checked ? "Способ 1 (прямой)" : "Способ 2 (цикл)";
if (rbMethod1.Checked)
{
// Способ 1
W = a * x + b * y + c * z;
}
else
{
// Способ 2 — через цикл
double[] coeffs = { a, b, c };
double[] vars = { x, y, z };
for (int i = 0; i < 3; i++)
{
W += coeffs[i] * vars[i];
}
}
string resultStr = $"{method} → W = {W:F4}";
lbHistory.Items.Add(resultStr);
lblResult.Text = $"Результат: W = {W:F4}";
lblResult.ForeColor = Color.DarkGreen;
}
catch (FormatException)
{
MessageBox.Show("Ошибка формата числа!\n\n" +
"• Убедитесь, что коэффициенты a, b, c выбраны из списков\n" +
"• В поля x, y, z введены только цифры (можно использовать запятую или точку)",
"Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Error);
lblResult.ForeColor = Color.Red;
lblResult.Text = "Ошибка ввода данных!";
}
catch (Exception ex)
{
MessageBox.Show("Непредвиденная ошибка: " + ex.Message,
"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
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(760, 660);
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
{
Text = "Лабораторная работа №16\nМеханизм событий. Простые элементы управления\nВариант 10",
Font = new System.Drawing.Font("Segoe UI", 15F, System.Drawing.FontStyle.Bold),
TextAlign = System.Drawing.ContentAlignment.MiddleCenter,
Location = new System.Drawing.Point(20, 10),
Size = new System.Drawing.Size(720, 80),
ForeColor = System.Drawing.Color.DarkBlue
};
// Группа способа расчёта
this.gbMethod = new GroupBox
{
Text = "Способ расчёта",
Location = new System.Drawing.Point(30, 100),
Size = new System.Drawing.Size(700, 115),
Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold)
};
this.lblMethodInfo = new Label
{
Text = "Выбран способ: Прямое вычисление",
Location = new System.Drawing.Point(20, 25),
Size = new System.Drawing.Size(500, 20)
};
this.rbMethod1 = new RadioButton
{
Text = "Способ 1: Прямое вычисление W = a·x + b·y + c·z",
Location = new System.Drawing.Point(20, 50),
AutoSize = true
};
this.rbMethod1.CheckedChanged += new System.EventHandler(this.rbMethod1_CheckedChanged);
this.rbMethod2 = new RadioButton
{
Text = "Способ 2: Через массив коэффициентов и цикл for",
Location = new System.Drawing.Point(20, 75),
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
{
Text = "Коэффициенты a, b, c",
Location = new System.Drawing.Point(30, 225),
Size = new System.Drawing.Size(355, 150),
Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold)
};
this.lblA = new Label { Text = "a =", Location = new Point(25, 35), AutoSize = true };
this.cbA = new ComboBox { Location = new Point(80, 32), Width = 130, DropDownStyle = ComboBoxStyle.DropDownList };
this.lblB = new Label { Text = "b =", Location = new Point(25, 70), AutoSize = true };
this.cbB = new ComboBox { Location = new Point(80, 67), Width = 130, DropDownStyle = ComboBoxStyle.DropDownList };
this.lblC = new Label { Text = "c =", Location = new Point(25, 105), AutoSize = true };
this.cbC = new ComboBox { Location = new Point(80, 102), Width = 130, 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
{
Text = "Переменные x, y, z",
Location = new System.Drawing.Point(400, 225),
Size = new System.Drawing.Size(330, 150),
Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold)
};
this.lblX = new Label { Text = "x =", Location = new Point(25, 35), AutoSize = true };
this.tbX = new TextBox { Location = new Point(80, 32), Width = 120, TextAlign = HorizontalAlignment.Center };
this.lblY = new Label { Text = "y =", Location = new Point(25, 70), AutoSize = true };
this.tbY = new TextBox { Location = new Point(80, 67), Width = 120, TextAlign = HorizontalAlignment.Center };
this.lblZ = new Label { Text = "z =", Location = new Point(25, 105), AutoSize = true };
this.tbZ = new TextBox { Location = new Point(80, 102), Width = 120, TextAlign = HorizontalAlignment.Center };
this.cbIncludeZ = new CheckBox
{
Text = "Включить z в расчёт",
Location = new Point(25, 135),
Checked = true,
Font = new Font("Segoe UI", 9.5F)
};
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
{
Text = "Вычислить W",
Location = new System.Drawing.Point(30, 390),
Size = new System.Drawing.Size(190, 50),
Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Bold),
BackColor = System.Drawing.Color.FromArgb(0, 122, 204),
ForeColor = System.Drawing.Color.White
};
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
this.btnClear = new Button
{
Text = "Очистить историю",
Location = new System.Drawing.Point(240, 390),
Size = new System.Drawing.Size(160, 50),
Font = new System.Drawing.Font("Segoe UI", 10F)
};
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
// Группа результата
this.gbResult = new GroupBox
{
Text = "Результат расчёта",
Location = new System.Drawing.Point(30, 455),
Size = new System.Drawing.Size(700, 85),
Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Bold)
};
this.lblResult = new Label
{
Text = "Результат: W = ?",
Location = new System.Drawing.Point(20, 30),
Size = new System.Drawing.Size(660, 40),
Font = new System.Drawing.Font("Segoe UI", 14F, System.Drawing.FontStyle.Bold),
ForeColor = System.Drawing.Color.DarkGreen
};
this.gbResult.Controls.Add(this.lblResult);
// История расчётов
this.lbHistory = new ListBox
{
Location = new System.Drawing.Point(30, 555),
Size = new System.Drawing.Size(700, 90),
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);
}
}
}