Rev 2374 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2374 | Rev 2377 | ||
---|---|---|---|
Line 21... | Line 21... | ||
21 | */ |
21 | */ |
22 | using System; |
22 | using System; |
23 | using System.Windows; |
23 | using System.Windows; |
24 | using System.Windows.Controls; |
24 | using System.Windows.Controls; |
25 | using System.ComponentModel; |
25 | using System.ComponentModel; |
- | 26 | using System.Windows.Input; |
|
Line 26... | Line 27... | ||
26 | 27 | ||
27 | namespace KeyPad |
28 | namespace KeyPad |
28 | { |
29 | { |
29 | /// <summary> |
30 | /// <summary> |
Line 33... | Line 34... | ||
33 | { |
34 | { |
Line 34... | Line 35... | ||
34 | 35 | ||
35 | private string _result; |
36 | private string _result; |
36 | int length = 0; |
37 | int length = 0; |
- | 38 | double min = 0, max = 0; |
|
37 | double min = 0, max = 0; |
39 | bool bDec = true; |
38 | #region Public Properties |
40 | #region Public Properties |
39 | public double MIN |
41 | public double MIN |
40 | { |
42 | { |
41 | get { return min; } |
43 | get { return min; } |
Line 57... | Line 59... | ||
57 | set { _result = value; this.OnPropertyChanged("Result"); } |
59 | set { _result = value; this.OnPropertyChanged("Result"); } |
58 | } |
60 | } |
59 | public void disableDecimal() |
61 | public void disableDecimal() |
60 | { |
62 | { |
61 | gDecimal.Visibility = Visibility.Hidden; |
63 | gDecimal.Visibility = Visibility.Hidden; |
- | 64 | bDec = false; |
|
62 | } |
65 | } |
63 | #endregion |
66 | #endregion |
Line 64... | Line 67... | ||
64 | 67 | ||
65 | public Keypad(Window owner) |
68 | public Keypad(Window owner) |
Line 82... | Line 85... | ||
82 | this.DialogResult = false; |
85 | this.DialogResult = false; |
83 | break; |
86 | break; |
Line 84... | Line 87... | ||
84 | 87 | ||
85 | case "RETURN": |
88 | case "RETURN": |
- | 89 | double result1; |
|
86 | double result1; |
90 | Result = LeaveOnlyNumbers(Result); |
87 | if (Double.TryParse(Result, out result1)) |
91 | if (Double.TryParse(Result, out result1)) |
88 | if (result1 <= max && result1 >= min && Result.Length <= length) |
92 | if (result1 <= max && result1 >= min && Result.Length <= length) |
89 | this.DialogResult = true; |
93 | this.DialogResult = true; |
Line 94... | Line 98... | ||
94 | Result = Result.Remove(Result.Length - 1); |
98 | Result = Result.Remove(Result.Length - 1); |
95 | break; |
99 | break; |
Line 96... | Line 100... | ||
96 | 100 | ||
97 | default: |
101 | default: |
98 | string tmp = Result + button.Content.ToString(); |
102 | string tmp = Result + button.Content.ToString(); |
99 | double result; |
- | |
100 | // if (Double.TryParse(tmp,System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"),out result)) |
- | |
101 | if (Double.TryParse(tmp,out result)) |
- | |
102 | { |
- | |
103 | if (result <= max && tmp.Length <= length) |
103 | if(checkValid(tmp)) |
104 | Result += button.Content.ToString(); |
- | |
105 | } |
104 | Result += button.Content.ToString(); |
106 | break; |
105 | break; |
107 | } |
106 | } |
- | 107 | } |
|
- | 108 | ||
- | 109 | private string LeaveOnlyNumbers(String inString) |
|
- | 110 | { |
|
- | 111 | string pattern; |
|
- | 112 | if (bDec) |
|
- | 113 | pattern = @"(0(?![0-9])|([1-9]\d*))+(\,\d+)?"; |
|
- | 114 | else |
|
- | 115 | pattern = @"(0(?![0-9])|([1-9]\d*))+"; |
|
- | 116 | string tmp; |
|
- | 117 | tmp = System.Text.RegularExpressions.Regex.Match(inString, pattern).Value; |
|
- | 118 | return tmp; |
|
- | 119 | } |
|
Line 108... | Line 120... | ||
108 | } |
120 | |
Line 109... | Line 121... | ||
109 | 121 | ||
- | 122 | #region INotifyPropertyChanged members |
|
- | 123 | ||
110 | #region INotifyPropertyChanged members |
124 | public event PropertyChangedEventHandler PropertyChanged; |
111 | 125 | ||
112 | public event PropertyChangedEventHandler PropertyChanged; |
126 | |
113 | private void OnPropertyChanged(String info) |
127 | private void OnPropertyChanged(String info) |
114 | { |
128 | { |
115 | if (PropertyChanged != null) |
129 | if (PropertyChanged != null) |
116 | { |
130 | { |
- | 131 | PropertyChanged(this, new PropertyChangedEventArgs(info)); |
|
- | 132 | } |
|
- | 133 | } |
|
- | 134 | #endregion |
|
- | 135 | private bool IsNumberKey(Key inKey) |
|
- | 136 | { |
|
- | 137 | if ((inKey < Key.D0 || inKey > Key.D9) & (inKey != Key.Decimal && inKey != Key.OemComma)) |
|
- | 138 | { |
|
- | 139 | if ((inKey < Key.NumPad0 || inKey > Key.NumPad9) & (inKey != Key.Decimal && inKey != Key.OemComma)) |
|
- | 140 | { |
|
- | 141 | return false; |
|
- | 142 | } |
|
- | 143 | } |
|
- | 144 | if ((inKey == Key.Decimal || inKey == Key.OemComma) & (textBox.Text.Contains(",")||!bDec)) |
|
- | 145 | return false; |
|
- | 146 | ||
- | 147 | return true; |
|
- | 148 | } |
|
- | 149 | ||
- | 150 | private bool IsDelOrBackspaceOrTabKey(Key inKey) |
|
- | 151 | { |
|
- | 152 | return inKey == Key.Delete || inKey == Key.Back; |
|
- | 153 | } |
|
- | 154 | ||
- | 155 | bool checkValid(string tmp) |
|
- | 156 | { |
|
- | 157 | double result; |
|
- | 158 | if (Double.TryParse(tmp, out result)) |
|
- | 159 | { |
|
- | 160 | if (result <= max && tmp.Length <= length) |
|
- | 161 | return true; |
|
- | 162 | } |
|
- | 163 | ||
- | 164 | return false; |
|
- | 165 | } |
|
- | 166 | #region Event Functions |
|
- | 167 | protected void OnKeyDown(object sender, KeyEventArgs e) |
|
- | 168 | { |
|
- | 169 | if (e.Key == System.Windows.Input.Key.Return) |
|
- | 170 | button_Click(button16, null); |
|
- | 171 | else |
|
- | 172 | { |
|
- | 173 | if (!IsNumberKey(e.Key) && !IsDelOrBackspaceOrTabKey(e.Key)) |
|
- | 174 | e.Handled = true; |
|
- | 175 | else |
|
- | 176 | { |
|
- | 177 | if ((e.Key != Key.Decimal && e.Key != Key.OemComma)) |
|
- | 178 | { |
|
- | 179 | string tmp = e.Key.ToString().Replace("NumPad", ""); |
|
- | 180 | tmp = tmp.Replace("D", ""); |
|
- | 181 | tmp = ((TextBox)sender).Text + tmp; |
|
- | 182 | if (checkValid(tmp)) |
|
- | 183 | e.Handled = false; |
|
- | 184 | else |
|
- | 185 | e.Handled = true; |
|
- | 186 | } |
|
- | 187 | } |
|
- | 188 | } |
|
- | 189 | } |
|
- | 190 | ||
- | 191 | protected void OnTextChanged(object sender, TextChangedEventArgs e) |
|
Line 117... | Line 192... | ||
117 | PropertyChanged(this, new PropertyChangedEventArgs(info)); |
192 | { |
Line 118... | Line 193... | ||
118 | } |
193 | Result = ((TextBox)sender).Text; |
119 | } |
194 | } |
120 | 195 | ||
121 | #endregion |
196 | #endregion |