Get up to 80 % extra points for free! More info:

Discussion: Lesson 5 - Code-Behind in C# .NET WPF and finishing the calculator

Back

 

Comments
Avatar
Stephen Cutts
Member
Avatar
Stephen Cutts:10/30/2021 22:09

So I decided to experiment with the code at this point. Here's what I did. Handled if user try's to enter letters, leaves blank, and pasting, Made a few methods for repeated code and clear text

using System;
using System.Windows;
using System.Windows.Input;
using System.Text.RegularExpressions;

namespace Calculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void number1TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            AllowableInput(e);
        }

        private void number2TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            AllowableInput(e);
        }

        private void number1TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            HandlePasting(e);
        }

        private void number2TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            HandlePasting(e);
        }

        private void calculateButton_Click(object sender, RoutedEventArgs e)
        {
            // preparing variables
            string operation = operationComboBox.Text;
            if (number1TextBox.Text != "" || number2TextBox.Text != "")
            {
                double number1 = double.Parse(number1TextBox.Text);
                double number2 = double.Parse(number2TextBox.Text);
                double result = 0;

                // calculations
                if (operation == "+")
                    result = number1 + number2;
                else if (operation == "-")
                    result = number1 - number2;
                else if (operation == "*")
                    result = number1 * number2;
                else if (operation == "/")
                {
                    if (number2 != 0)
                        result = number1 / number2;
                    else
                        MessageBox.Show("You cannot divide by zero");
                }
                resultTextBlock.Text = result.ToString();
                ResetText();
            }
            else
            {
                ResetText();
            }

        }

        public void AllowableInput(TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
        }

        public void HandlePasting(DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsTextAllowed(text))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }

        private static readonly Regex _regex = new Regex("[^0-9.-]+");
        private static bool IsTextAllowed(string text)
        {
            return !_regex.IsMatch(text);
        }

        public void ResetText()
        {
            number1TextBox.Text = "0";
            number2TextBox.Text = "0";
        }

    }

}
Reply
10/30/2021 22:09
YOLO
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

1 messages from 1 displayed.