Subversion Repositories Projects

Rev

Rev 2374 | Blame | Last modification | View Log | RSS feed



/*
 * Copyright (c) 2008, Andrzej Rusztowicz (ekus.net)
* All rights reserved.

* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* 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.

* 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.

* 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.
*/

/*
 * Added by Michele Cattafesta (mesta-automation.com) 29/2/2011
 * The code has been totally rewritten to create a control that can be modified more easy even without knowing the MVVM pattern.
 * If you need to check the original source code you can download it here: http://wosk.codeplex.com/
 */

using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Input;

namespace KeyPad
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class Keypad : Window,INotifyPropertyChanged
    {

        private string _result;
        int length = 0;
        double min = 0, max = 0;
        bool bDec = true;
        #region Public Properties
        public double MIN
        {
            get { return min; }
            set { min = value; }
        }
        public double MAX
        {
            get { return max; }
            set { max = value; }
        }
        public int LENGTH
        {
            get { return length; }
            set { length = value; }
        }
        public string Result
        {
            get { return _result; }
            set { _result = value; this.OnPropertyChanged("Result"); }
        }
        public void disableDecimal()
        {
            gDecimal.Visibility = Visibility.Hidden;
            bDec = false;
        }
        #endregion
       
        public Keypad(Window owner)
        {
            InitializeComponent();
            this.Owner = owner;
            this.DataContext = this;            
        }        

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            switch (button.CommandParameter.ToString())
            {
                case "Clear":
                    Result = "";
                    break;

                case "ESC":
                    this.DialogResult = false;
                    break;

                case "RETURN":
                    double result1;
                    Result = LeaveOnlyNumbers(Result);
                    if (Double.TryParse(Result, out result1))
                        if (result1 <= max && result1 >= min && Result.Length <= length)
                            this.DialogResult = true;
                    break;

                case "BACK":
                    if (Result.Length > 0)
                        Result = Result.Remove(Result.Length - 1);
                    break;

                default:
                    string tmp = Result + button.Content.ToString();
                    if(checkValid(tmp))
                        Result += button.Content.ToString();
                    break;
            }  
        }

        private string LeaveOnlyNumbers(String inString)
        {
            string pattern;
            if (bDec)
                pattern = @"(0(?![0-9])|([1-9]\d*))+(\,\d+)?";
            else
                pattern = @"(0(?![0-9])|([1-9]\d*))+";
            string tmp;
            tmp = System.Text.RegularExpressions.Regex.Match(inString, pattern).Value;
            return tmp;
        }


        #region INotifyPropertyChanged members

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
      private bool IsNumberKey(Key inKey)
        {
            if ((inKey < Key.D0 || inKey > Key.D9) & (inKey != Key.Decimal && inKey != Key.OemComma))
            {
                if ((inKey < Key.NumPad0 || inKey > Key.NumPad9) & (inKey != Key.Decimal && inKey != Key.OemComma))
                {
                    return false;
                }
            }
            if ((inKey == Key.Decimal || inKey == Key.OemComma) & (textBox.Text.Contains(",")||!bDec))
                return false;

            return true;
        }

        private bool IsDelOrBackspaceOrTabKey(Key inKey)
        {
            return inKey == Key.Delete || inKey == Key.Back;
        }

        bool checkValid(string tmp)
        {
            double result;
            if (Double.TryParse(tmp, out result))
            {
                if (result <= max && tmp.Length <= length)
                    return true;
            }

            return false;
        }
        #region Event Functions
        protected void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Return)
                button_Click(button16, null);
            else
            {
                if (!IsNumberKey(e.Key) && !IsDelOrBackspaceOrTabKey(e.Key))
                    e.Handled = true;
                else
                {
                    if ((e.Key != Key.Decimal && e.Key != Key.OemComma))
                    {
                        string tmp = e.Key.ToString().Replace("NumPad", "");
                        tmp = tmp.Replace("D", "");
                        tmp = ((TextBox)sender).Text + tmp;
                        if (checkValid(tmp))
                            e.Handled = false;
                        else
                            e.Handled = true;
                    }
                }
            }
        }

        protected void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            Result = ((TextBox)sender).Text;
        }

        #endregion



    }
}