Загрузка данных
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.Clear();
cbB.Items.Clear();
cbC.Items.Clear();
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 = 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];
}
string resultStr = $"{method} → W = {W:F4}";
lbHistory.Items.Add(resultStr);
lblResult.Text = $"Результат: W = {W:F4}";
lblResult.ForeColor = Color.DarkGreen;
}
catch (Exception)
{
MessageBox.Show("Ошибка ввода!\nПроверьте числа в полях.",
"Ошибка", 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)
{
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 Size(780, 670);
this.Text = "Лабораторная работа №16 — Вариант 10";
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.BackColor = Color.WhiteSmoke;
// Заголовок
this.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
};
// Способ расчёта
this.gbMethod = new GroupBox
{
Text = "Способ расчёта",
Location = new Point(30, 100),
Size = new Size(720, 115),
Font = new Font("Segoe UI", 10F, FontStyle.Bold)
};
this.lblMethodInfo = new Label { Text = "Выбран способ: Прямое вычисление", Location = new Point(20, 25), Size = new Size(500, 20) };
this.rbMethod1 = new RadioButton { Text = "Способ 1: Прямое вычисление W = a·x + b·y + c·z", Location = new Point(20, 50), AutoSize = true };
this.rbMethod1.CheckedChanged += rbMethod1_CheckedChanged;
this.rbMethod2 = new RadioButton { Text = "Способ 2: Через массив коэффициентов и цикл for", Location = new Point(20, 75), AutoSize = true };
this.rbMethod2.CheckedChanged += rbMethod2_CheckedChanged;
gbMethod.Controls.Add(lblMethodInfo);
gbMethod.Controls.Add(rbMethod1);
gbMethod.Controls.Add(rbMethod2);
// Коэффициенты
this.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)
};
this.lblA = new Label { Text = "a =", Location = new Point(30, 38), AutoSize = true };
this.cbA = new ComboBox { Location = new Point(85, 35), Width = 140, DropDownStyle = ComboBoxStyle.DropDownList, Font = new Font("Segoe UI", 10) };
this.lblB = new Label { Text = "b =", Location = new Point(30, 73), AutoSize = true };
this.cbB = new ComboBox { Location = new Point(85, 70), Width = 140, DropDownStyle = ComboBoxStyle.DropDownList, Font = new Font("Segoe UI", 10) };
this.lblC = new Label { Text = "c =", Location = new Point(30, 108), AutoSize = true };
this.cbC = new ComboBox { Location = new Point(85, 105), Width = 140, DropDownStyle = ComboBoxStyle.DropDownList, Font = new Font("Segoe UI", 10) };
// Важно: добавляем элементы в GroupBox
gbCoefficients.Controls.Add(lblA);
gbCoefficients.Controls.Add(cbA);
gbCoefficients.Controls.Add(lblB);
gbCoefficients.Controls.Add(cbB);
gbCoefficients.Controls.Add(lblC);
gbCoefficients.Controls.Add(cbC);
// Переменные
this.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)
};
this.lblX = new Label { Text = "x =", Location = new Point(30, 38), AutoSize = true };
this.tbX = new TextBox { Location = new Point(85, 35), Width = 120, TextAlign = HorizontalAlignment.Center };
this.lblY = new Label { Text = "y =", Location = new Point(30, 73), AutoSize = true };
this.tbY = new TextBox { Location = new Point(85, 70), Width = 120, TextAlign = HorizontalAlignment.Center };
this.lblZ = new Label { Text = "z =", Location = new Point(30, 108), AutoSize = true };
this.tbZ = new TextBox { Location = new Point(85, 105), Width = 120, TextAlign = HorizontalAlignment.Center };
this.cbIncludeZ = new CheckBox { Text = "Включить z в расчёт", Location = new Point(30, 135), Checked = true };
this.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);
// Кнопки
this.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
};
this.btnCalculate.Click += btnCalculate_Click;
this.btnClear = new Button
{
Text = "Очистить историю",
Location = new Point(250, 395),
Size = new Size(170, 50)
};
this.btnClear.Click += btnClear_Click;
// Результат
this.gbResult = new GroupBox
{
Text = "Результат расчёта",
Location = new Point(30, 460),
Size = new Size(720, 85),
Font = new Font("Segoe UI", 10F, FontStyle.Bold)
};
this.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);
// История
this.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);
}
}
}