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;
26
 
27
namespace KeyPad
28
{
29
    /// <summary>
30
    /// Logica di interazione per MainWindow.xaml
31
    /// </summary>
32
    public partial class Keypad : Window,INotifyPropertyChanged
33
    {
34
 
35
        private string _result;
2364 - 36
        int length = 0;
37
        double min = 0, max = 0;
38
        #region Public Properties
39
        public double MIN
40
        {
41
            get { return min; }
42
            set { min = value; }
43
        }
44
        public double MAX
45
        {
46
            get { return max; }
47
            set { max = value; }
48
        }
49
        public int LENGTH
50
        {
51
            get { return length; }
52
            set { length = value; }
53
        }
2357 - 54
        public string Result
55
        {
56
            get { return _result; }
57
            set { _result = value; this.OnPropertyChanged("Result"); }
58
        }
2364 - 59
        public void disableDecimal()
60
        {
61
            gDecimal.Visibility = Visibility.Hidden;
62
        }
2357 - 63
        #endregion
64
 
65
        public Keypad(Window owner)
66
        {
67
            InitializeComponent();
68
            this.Owner = owner;
69
            this.DataContext = this;            
70
        }        
71
 
72
        private void button_Click(object sender, RoutedEventArgs e)
73
        {
74
            Button button = sender as Button;
75
            switch (button.CommandParameter.ToString())
76
            {
77
                case "ESC":
78
                    this.DialogResult = false;
79
                    break;
80
 
81
                case "RETURN":
2364 - 82
                    double result1;
83
                    if (Double.TryParse(Result, out result1))
84
                        if (result1 <= max && result1 >= min && Result.Length <= length)
85
                            this.DialogResult = true;
2357 - 86
                    break;
87
 
88
                case "BACK":
89
                    if (Result.Length > 0)
90
                        Result = Result.Remove(Result.Length - 1);
91
                    break;
92
 
93
                default:
2364 - 94
                    string tmp = Result + button.Content.ToString();
95
                    double result;
96
//                    if (Double.TryParse(tmp,System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"),out result))
97
                    if (Double.TryParse(tmp,out result))
98
                    {
99
                        if (result <= max && tmp.Length <= length)
100
                        Result += button.Content.ToString();
101
                    }
2357 - 102
                    break;
103
            }  
104
        }    
105
 
106
        #region INotifyPropertyChanged members
107
 
108
        public event PropertyChangedEventHandler PropertyChanged;
109
        private void OnPropertyChanged(String info)
110
        {
111
            if (PropertyChanged != null)
112
            {
113
                PropertyChanged(this, new PropertyChangedEventArgs(info));
114
            }
115
        }
116
 
117
        #endregion
118
 
119
 
120
 
121
    }
122
}