-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
128 lines (117 loc) · 4.56 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CalculatorApp
{
public partial class Form1 : Form
{
private double result = 0;
private string operation = "";
private bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
InitializeCalculator();
this.Text = "Калькулятор"; // Название окна
this.BackColor = Color.LightGray; // Цвет фона формы
}
private void InitializeCalculator()
{
// Настройка экрана
TextBox textBoxDisplay = new TextBox
{
Name = "textBoxDisplay",
Text = "0",
ReadOnly = true,
Width = 200,
Height = 40,
Location = new Point(10, 10),
Font = new Font("Arial", 16),
TextAlign = HorizontalAlignment.Right
};
this.Controls.Add(textBoxDisplay);
// Настройка кнопок цифр и операций
string[] buttonTexts = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "C", "=", "+" };
int x = 10, y = 60;
for (int i = 0; i < buttonTexts.Length; i++)
{
Button button = new Button
{
Text = buttonTexts[i],
Width = 50,
Height = 50,
Location = new Point(x, y),
Font = new Font("Arial", 14),
BackColor = Color.LightSteelBlue, // Цвет кнопок
FlatStyle = FlatStyle.Flat // Плоский стиль кнопок
};
button.Click += Button_Click;
this.Controls.Add(button);
x += 50;
if ((i + 1) % 4 == 0)
{
x = 10;
y += 50;
}
}
// Подпись "Developed by Матвеева Дарья Алексеевна"
Label labelSignature = new Label
{
Text = "Developed by Матвеева Дарья Алексеевна",
AutoSize = true,
Location = new Point(10, y + 60),
Font = new Font("Arial", 10, FontStyle.Italic),
ForeColor = Color.DarkSlateGray
};
this.Controls.Add(labelSignature);
}
private void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
TextBox textBoxDisplay = (TextBox)this.Controls["textBoxDisplay"];
if (button.Text == "C")
{
textBoxDisplay.Text = "0";
result = 0;
operation = "";
isOperationPerformed = false;
return;
}
if (button.Text == "=")
{
switch (operation)
{
case "+": textBoxDisplay.Text = (result + Double.Parse(textBoxDisplay.Text)).ToString(); break;
case "-": textBoxDisplay.Text = (result - Double.Parse(textBoxDisplay.Text)).ToString(); break;
case "*": textBoxDisplay.Text = (result * Double.Parse(textBoxDisplay.Text)).ToString(); break;
case "/":
if (textBoxDisplay.Text != "0")
{
textBoxDisplay.Text = (result / Double.Parse(textBoxDisplay.Text)).ToString();
}
else
{
MessageBox.Show("Деление на ноль невозможно!");
}
break;
}
result = Double.Parse(textBoxDisplay.Text);
operation = "";
isOperationPerformed = true;
}
else if (button.Text == "+" || button.Text == "-" || button.Text == "*" || button.Text == "/")
{
operation = button.Text;
result = Double.Parse(textBoxDisplay.Text);
isOperationPerformed = true;
}
else
{
if ((textBoxDisplay.Text == "0") || isOperationPerformed)
textBoxDisplay.Text = "";
isOperationPerformed = false;
textBoxDisplay.Text += button.Text;
}
}
}
}