-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
390 lines (353 loc) · 11.9 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Calculator;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
namespace Calculator
{
public partial class Form1 : Form
{
private Value1 value1;
private ButtonClicks buttons;
private Value2 value2;
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
buttons = new ButtonClicks();
value1 = new Value1(double.NaN);
value2 = new Value2(double.NaN);
this.AcceptButton = equalButton;
button1.TabStop = false;
button2.TabStop = false;
button3.TabStop = false;
button4.TabStop = false;
button5.TabStop = false;
button6.TabStop = false;
button7.TabStop = false;
button8.TabStop = false;
button9.TabStop = false;
decimalButton.TabStop = false;
button11.TabStop = false;
equalButton.TabStop = false;
plusButton.TabStop = false;
minusButton.TabStop = false;
multiplyButton.TabStop = false;
divideButton.TabStop = false;
clearButton.TabStop = false;
clearEntry.TabStop = false;
}
private void Form1_KeyPress1(object sender, KeyPressEventArgs e)
{
throw new NotImplementedException();
}
/// <summary>
/// Checks to see if the label is empty
/// </summary>
private bool ValueCheck()
{
var t = false;
if (displayValue.Text == string.Empty)
{
t = true;
}
return t;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
// These buttons are just adding the numbers to the label
private void button1_Click(object sender, EventArgs e)
{
displayValue.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
displayValue.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
displayValue.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
displayValue.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
displayValue.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
displayValue.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
displayValue.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
displayValue.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
displayValue.Text += "9";
}
private void decimalButton_Click(object sender, EventArgs e)
{
displayValue.Text += ".";
}
private void button11_Click(object sender, EventArgs e)
{
displayValue.Text += "0";
}
// Operator logic
private void plusButton_Click(object sender, EventArgs e)
{
var isNull = ValueCheck();
if(isNull)
{
MessageBox.Show("Please enter a number");
}
else
{
value1.Value = Convert.ToDouble(displayValue.Text);
previousValueLabel.Text = value1.Value.ToString();
buttons.IsAddition = true;
displayValue.Text = string.Empty;
buttons.Check();
}
}
private void minusButton_Click(object sender, EventArgs e)
{
var isNull = ValueCheck();
if (isNull)
{
MessageBox.Show("Please enter a number");
}
else
{
value1.Value = Convert.ToDouble(displayValue.Text);
previousValueLabel.Text = value1.Value.ToString();
buttons.IsSubtraction = true;
displayValue.Text = string.Empty;
buttons.Check();
}
}
private void multiplyButton_Click(object sender, EventArgs e)
{
var isNull = ValueCheck();
if (isNull)
{
MessageBox.Show("Please enter a number");
}
else
{
buttons.IsMultiplication = true;
value1.Value = Convert.ToDouble(displayValue.Text);
previousValueLabel.Text = value1.Value.ToString();
displayValue.Text = string.Empty;
buttons.Check();
}
}
private void divideButton_Click(object sender, EventArgs e)
{
var isNull = ValueCheck();
if (isNull)
{
MessageBox.Show("Please enter a number");
}
else
{
value1.Value = Convert.ToDouble(displayValue.Text);
previousValueLabel.Text = value1.Value.ToString();
buttons.IsDivision = true;
displayValue.Text = string.Empty;
buttons.Check();
}
}
private void equalButton_Click(object sender, EventArgs e)
{
try
{
var isNull = ValueCheck();
if (isNull)
{
throw new Exception("Secondary value was not provided");
}
value2.Value = Convert.ToDouble(displayValue.Text);
if (value2.Value == double.NaN)
{
throw new Exception("Secondary value was not provided");
}
double value = double.NaN;
if (value1.Value == double.NaN)
{
throw new Exception("No inital value was provided");
}
else
{
if ((bool)buttons.IsSubtraction)
{
previousValueLabel.Text = value1.Value.ToString() + " - " + value2.Value.ToString();
value = value1.Value - value2.Value;
buttons.Reset();
}
else if ((bool)buttons.IsMultiplication)
{
previousValueLabel.Text = value1.Value.ToString() + " * " + value2.Value.ToString();
value = value1.Value * value2.Value;
buttons.Reset();
}
else if ((bool)buttons.IsDivision)
{
if(value2.Value != 0)
{
previousValueLabel.Text = value1.Value.ToString() + " / " + value2.Value.ToString();
value = value1.Value / value2.Value;
}
else
{
throw new Exception("Division by 0 is not allowed");
}
buttons.Reset();
}
else if ((bool)buttons.IsAddition)
{
previousValueLabel.Text = value1.Value.ToString() + " + " + value2.Value.ToString();
value = value1.Value + value2.Value;
buttons.Reset();
}
value1.Value = value;
displayValue.Text = value1.Value.ToString();
value2.Value = double.NaN;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// For keybinding
// Reference: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=windowsdesktop-8.0
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"KeyCode: {e.KeyCode}, KeyValue: {e.KeyValue}");
if(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
equalButton_Click(sender, e);
}
else if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1)
{
displayValue.Text += "1";
}
else if (e.KeyCode == Keys.NumPad2 || e.KeyCode == Keys.D2)
{
displayValue.Text += "2";
}
else if (e.KeyCode == Keys.NumPad3 || e.KeyCode == Keys.D3)
{
displayValue.Text += "3";
}
else if (e.KeyCode == Keys.NumPad4 || e.KeyCode == Keys.D4)
{
displayValue.Text += "4";
}
else if (e.KeyCode == Keys.NumPad5 || e.KeyCode == Keys.D5)
{
displayValue.Text += "5";
}
else if (e.KeyCode == Keys.NumPad6 || e.KeyCode == Keys.D6)
{
displayValue.Text += "6";
}
else if (e.KeyCode == Keys.NumPad7 || e.KeyCode == Keys.D7)
{
displayValue.Text += "7";
}
else if (e.KeyCode == Keys.NumPad8 || e.KeyCode == Keys.D8)
{
displayValue.Text += "8";
}
else if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9)
{
displayValue.Text += "9";
}
else if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
displayValue.Text += "0";
}
else if (e.KeyCode == Keys.Add)
{
plusButton_Click(sender, e);
}
else if (e.KeyCode == Keys.Subtract)
{
minusButton_Click(sender, e);
}
else if(e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
var length = displayValue.Text.Length;
if (length > 0)
{
displayValue.Text = displayValue.Text.Substring(0, length - 1);
}
}
else if(e.KeyCode == Keys.M)
{
var text = displayValue.Text;
var newText = "-" + text;
displayValue.Text = newText;
}
else if(e.KeyCode == Keys.C)
{
clearButton_Click(sender, e);
}
else if(e.KeyCode == Keys.Z)
{
clearEntry_Click(sender, e);
}
else if(e.KeyCode == Keys.Multiply)
{
multiplyButton_Click(sender, e);
}
else if(e.KeyCode== Keys.Divide)
{
divideButton_Click(sender, e);
}
else if(e.KeyCode == Keys.Decimal)
{
decimalButton_Click(sender, e);
}
}
// Clearing and function logic
private void clearButton_Click(object sender, EventArgs e)
{
value1.Value = double.NaN;
value2.Value = double.NaN;
displayValue.Text = string.Empty;
}
private void clearEntry_Click(object sender, EventArgs e)
{
if (value2.Value != double.NaN)
{
value2.Value = double.NaN;
displayValue.Text = value1.Value.ToString();
}
}
// Key Binds form
private KeyBinds form2;
private void keyBindsToolStripMenuItem_Click(object sender, EventArgs e)
{
form2 = new KeyBinds();
form2.Show();
}
}
}