Beispiel #1
0
        private static void OnCalculatorButtonTypeChanged(DependencyObject o, CalculatorButtonType oldValue, CalculatorButtonType newValue)
        {
            Button button = o as Button;

            button.CommandParameter = newValue;
            button.Content          = CalculatorUtilities.GetCalculatorButtonContent(newValue);
        }
Beispiel #2
0
        private void ProcessMemoryKey(Calculator.CalculatorButtonType buttonType)
        {
            decimal currentValue = CalculatorUtilities.ParseDecimal(DisplayText);

            _showNewNumber = true;

            switch (buttonType)
            {
            case Calculator.CalculatorButtonType.MAdd:
                Memory += currentValue;
                break;

            case Calculator.CalculatorButtonType.MC:
                Memory = decimal.Zero;
                break;

            case Calculator.CalculatorButtonType.MR:
                DisplayText    = Memory.ToString();
                _showNewNumber = false;
                break;

            case Calculator.CalculatorButtonType.MS:
                Memory = currentValue;
                break;

            case Calculator.CalculatorButtonType.MSub:
                Memory -= currentValue;
                break;

            default:
                break;
            }
        }
Beispiel #3
0
        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            var buttonType = CalculatorUtilities.GetCalculatorButtonTypeFromText(e.Text);

            if (buttonType != CalculatorButtonType.None)
            {
                SimulateCalculatorButtonClick(buttonType);
                ProcessCalculatorButton(buttonType);
            }
        }
 protected override void OnTextInput(TextCompositionEventArgs e)
 {
     if (IsOpen && EnterClosesCalculator)
     {
         var buttonType = CalculatorUtilities.GetCalculatorButtonTypeFromText(e.Text);
         if (buttonType == Calculator.CalculatorButtonType.Equal)
         {
             CloseCalculatorUpDown(true);
         }
     }
 }
Beispiel #5
0
        private void ProcessDigitKey(CalculatorButtonType buttonType)
        {
            if (_showNewNumber)
            {
                DisplayText = CalculatorUtilities.GetCalculatorButtonContent(buttonType);
            }
            else
            {
                DisplayText += CalculatorUtilities.GetCalculatorButtonContent(buttonType);
            }

            _showNewNumber = false;
        }
Beispiel #6
0
 private void ProcessCalculatorButton(CalculatorButtonType buttonType)
 {
     if (CalculatorUtilities.IsDigit(buttonType))
     {
         ProcessDigitKey(buttonType);
     }
     else if ((CalculatorUtilities.IsMemory(buttonType)))
     {
         ProcessMemoryKey(buttonType);
     }
     else
     {
         ProcessOperationKey(buttonType);
     }
 }
Beispiel #7
0
        private decimal CalculateValue(Operation operation)
        {
            decimal newValue     = decimal.Zero;
            decimal currentValue = CalculatorUtilities.ParseDecimal(DisplayText);

            switch (operation)
            {
            case Operation.Add:
                newValue = CalculatorUtilities.Add(_previousValue, currentValue);
                break;

            case Operation.Subtract:
                newValue = CalculatorUtilities.Subtract(_previousValue, currentValue);
                break;

            case Operation.Multiply:
                newValue = CalculatorUtilities.Multiply(_previousValue, currentValue);
                break;

            case Operation.Divide:
                newValue = CalculatorUtilities.Divide(_previousValue, currentValue);
                break;

            //case Operation.Percent:
            //    newValue = CalculatorUtilities.Percent(_previousValue, currentValue);
            //    break;
            case Operation.Sqrt:
                newValue = CalculatorUtilities.SquareRoot(currentValue);
                break;

            case Operation.Fraction:
                newValue = CalculatorUtilities.Fraction(currentValue);
                break;

            case Operation.Negate:
                newValue = CalculatorUtilities.Negate(currentValue);
                break;

            default:
                newValue = decimal.Zero;
                break;
            }

            return(newValue);
        }
Beispiel #8
0
        private void SimulateCalculatorButtonClick(CalculatorButtonType buttonType)
        {
            var button = CalculatorUtilities.FindButtonByCalculatorButtonType(_buttonPanel, buttonType);

            if (button != null)
            {
                VisualStateManager.GoToState(button, "Pressed", true);
                DispatcherTimer timer;
                if (_timers.ContainsKey(button))
                {
                    timer = _timers[button];
                    timer.Stop();
                }
                else
                {
                    timer          = new DispatcherTimer();
                    timer.Interval = TimeSpan.FromMilliseconds(100);
                    timer.Tick    += Timer_Tick;
                    _timers.Add(button, timer);
                }

                timer.Start();
            }
        }
Beispiel #9
0
        private void ProcessOperationKey(CalculatorButtonType buttonType)
        {
            switch (buttonType)
            {
            case CalculatorButtonType.Add:
                Calculate(Operation.Add);
                break;

            case CalculatorButtonType.Subtract:
                Calculate(Operation.Subtract);
                break;

            case CalculatorButtonType.Multiply:
                Calculate(Operation.Multiply);
                break;

            case CalculatorButtonType.Divide:
                Calculate(Operation.Divide);
                break;

            case CalculatorButtonType.Percent:
                if (_lastOperation != Operation.None)
                {
                    decimal currentValue = CalculatorUtilities.ParseDecimal(DisplayText);
                    decimal newValue     = CalculatorUtilities.Percent(_previousValue, currentValue);
                    DisplayText = newValue.ToString();
                }
                else
                {
                    DisplayText    = "0";
                    _showNewNumber = true;
                }
                return;

            case CalculatorButtonType.Sqrt:
                Calculate(Operation.Sqrt, Operation.None);
                break;

            case CalculatorButtonType.Fraction:
                Calculate(Operation.Fraction, Operation.None);
                break;

            case CalculatorButtonType.Negate:
                Calculate(Operation.Negate, Operation.None);
                break;

            case CalculatorButtonType.Equal:
                Calculate(Operation.None);
                break;

            case CalculatorButtonType.Clear:
                Calculate(Operation.Clear, Operation.None);
                break;

            case CalculatorButtonType.Cancel:
                DisplayText    = _previousValue.ToString();
                _lastOperation = Operation.None;
                _showNewNumber = true;
                return;

            case CalculatorButtonType.Back:
                ProcessBackKey();
                return;

            default:
                break;
            }

            Decimal.TryParse(DisplayText, out _previousValue);
            _showNewNumber = true;
        }
 private void SimulateCalculatorButtonClick(CalculatorButtonType buttonType)
 {
     _calculatorButton = CalculatorUtilities.FindButtonByCalculatorButtonType(_buttonPanel, buttonType);
     VisualStateManager.GoToState(_calculatorButton, "Pressed", true);
     _timer.Start();
 }