-
Notifications
You must be signed in to change notification settings - Fork 5
/
MainWindow.xaml.cs
157 lines (131 loc) · 4.99 KB
/
MainWindow.xaml.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
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFCalc.Utils;
namespace WPFCalc
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string DecimalSeparator => CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
decimal FirstValue { get; set; }
decimal? SecondValue { get; set; }
IOperation CurrentOperation;
public MainWindow()
{
InitializeComponent();
btnPoint.Content = DecimalSeparator;
btnSum.Tag = new Sum();
btnSubtraction.Tag = new Subtraction();
btnDivision.Tag = new Division();
btnMultiplication.Tag = new Multiplication();
}
private void regularButtonClick(object sender, RoutedEventArgs e)
=> SendToInput(((Button)sender).Content.ToString());
private void SendToInput(string content)
{
//Prevent 0 from appearing on the left of new numbers
if (txtInput.Text == "0")
txtInput.Text = "";
txtInput.Text = $"{txtInput.Text}{content}";
}
private void btnPoint_Click(object sender, RoutedEventArgs e)
{
if (txtInput.Text.Contains(this.DecimalSeparator))
return;
regularButtonClick(sender, e);
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
//Prevent from clearing zero
if (txtInput.Text == "0")
return;
txtInput.Text = txtInput.Text.Substring(0, txtInput.Text.Length - 1);
if (txtInput.Text == "")
txtInput.Text = "0";
}
private void operationButton_Click(object sender, RoutedEventArgs e)
{
//if current operation is not null then we already have the FirstValue
if (CurrentOperation == null)
FirstValue = Convert.ToDecimal(txtInput.Text);
CurrentOperation = (IOperation)((Button)sender).Tag;
SecondValue = null;
txtInput.Text = "";
}
private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
switch (e.Text)
{
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
SendToInput(e.Text);
break;
case "*":
btnMultiplication.PerformClick();
break;
case "-":
btnSubtraction.PerformClick();
break;
case "+":
btnSum.PerformClick();
break;
case "/":
btnDivision.PerformClick();
break;
case "=":
btnEquals.PerformClick();
break;
default:
//Can't use directly from switch because it is not a constant
if (e.Text == DecimalSeparator)
btnPoint.PerformClick();
else if (e.Text[0] == (char)8)
btnBack.PerformClick();
else if (e.Text[0] == (char)13)
btnEquals.PerformClick();
break;
}
//This will prevent other buttons focus firing its click event on <ENTER> while typing
btnEquals.Focus();
}
private void btnEquals_Click(object sender, RoutedEventArgs e)
{
if (CurrentOperation == null)
return;
if (txtInput.Text == "")
return;
//SecondValue is used for multiple clicks on Equals bringing the newest result of last operation
decimal val2 = SecondValue ?? Convert.ToDecimal(txtInput.Text);
try
{
txtInput.Text = (FirstValue = CurrentOperation.DoOperation(FirstValue, (decimal)(SecondValue = val2))).ToString();
}
catch(DivideByZeroException)
{
MessageBox.Show("Can't divide by zero", "Divided by zero", MessageBoxButton.OK, MessageBoxImage.Error);
btnClearAll.PerformClick();
}
}
private void btnClearEntry_Click(object sender, RoutedEventArgs e)
=> txtInput.Text = "0";
private void btnClearAll_Click(object sender, RoutedEventArgs e)
{
FirstValue = 0;
CurrentOperation = null;
txtInput.Text = "0";
}
}
}