Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2357 - 1

2
 
3
/*
4
 * Copyright (c) 2008, Andrzej Rusztowicz (ekus.net)
5
* All rights reserved.
6
 
7
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8
 
9
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10
 
11
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12
 
13
* Neither the name of ekus.net nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
14
 
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
*/
17
/*
18
 * Added by Michele Cattafesta (mesta-automation.com) 29/2/2011
19
 * The code has been totally rewritten to create a control that can be modified more easy even without knowing the MVVM pattern.
20
 * If you need to check the original source code you can download it here: http://wosk.codeplex.com/
21
 */
22
using System;
23
using System.Windows;
24
using System.Windows.Controls;
25
using System.ComponentModel;
2377 - 26
using System.Windows.Input;
2357 - 27
 
28
namespace KeyPad
29
{
30
    /// <summary>
31
    /// Logica di interazione per MainWindow.xaml
32
    /// </summary>
33
    public partial class Keypad : Window,INotifyPropertyChanged
34
    {
35
 
36
        private string _result;
2364 - 37
        int length = 0;
38
        double min = 0, max = 0;
2377 - 39
        bool bDec = true;
2364 - 40
        #region Public Properties
41
        public double MIN
42
        {
43
            get { return min; }
44
            set { min = value; }
45
        }
46
        public double MAX
47
        {
48
            get { return max; }
49
            set { max = value; }
50
        }
51
        public int LENGTH
52
        {
53
            get { return length; }
54
            set { length = value; }
55
        }
2357 - 56
        public string Result
57
        {
58
            get { return _result; }
59
            set { _result = value; this.OnPropertyChanged("Result"); }
60
        }
2364 - 61
        public void disableDecimal()
62
        {
63
            gDecimal.Visibility = Visibility.Hidden;
2377 - 64
            bDec = false;
2364 - 65
        }
2357 - 66
        #endregion
67
 
68
        public Keypad(Window owner)
69
        {
70
            InitializeComponent();
71
            this.Owner = owner;
72
            this.DataContext = this;            
73
        }        
74
 
75
        private void button_Click(object sender, RoutedEventArgs e)
76
        {
77
            Button button = sender as Button;
78
            switch (button.CommandParameter.ToString())
79
            {
2374 - 80
                case "Clear":
81
                    Result = "";
82
                    break;
83
 
2357 - 84
                case "ESC":
85
                    this.DialogResult = false;
86
                    break;
87
 
88
                case "RETURN":
2364 - 89
                    double result1;
2377 - 90
                    Result = LeaveOnlyNumbers(Result);
2364 - 91
                    if (Double.TryParse(Result, out result1))
92
                        if (result1 <= max && result1 >= min && Result.Length <= length)
93
                            this.DialogResult = true;
2357 - 94
                    break;
95
 
96
                case "BACK":
97
                    if (Result.Length > 0)
98
                        Result = Result.Remove(Result.Length - 1);
99
                    break;
100
 
101
                default:
2364 - 102
                    string tmp = Result + button.Content.ToString();
2377 - 103
                    if(checkValid(tmp))
2364 - 104
                        Result += button.Content.ToString();
2357 - 105
                    break;
106
            }  
2377 - 107
        }
2357 - 108
 
2377 - 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
        }
120
 
121
 
2357 - 122
        #region INotifyPropertyChanged members
123
 
124
        public event PropertyChangedEventHandler PropertyChanged;
2377 - 125
 
126
 
2357 - 127
        private void OnPropertyChanged(String info)
128
        {
129
            if (PropertyChanged != null)
130
            {
131
                PropertyChanged(this, new PropertyChangedEventArgs(info));
132
            }
133
        }
2377 - 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;
2357 - 146
 
2377 - 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)
192
        {
193
            Result = ((TextBox)sender).Text;
194
        }
195
 
2357 - 196
        #endregion
197
 
2377 - 198
 
199
 
2357 - 200
    }
201
}