private void CalcButton_Click(object sender, RoutedEventArgs e)
        {
            can = false;

            CalcButton a = (CalcButton)e.Source;

            if (a.Type == "number" || a.Type == "passive")
            {
                test += a.ActionValue;
            }

            if (a.Type == "active")
            {
                if (a.ActionValue == "del")
                {
                    if (test.Length >= 1)
                    {
                        test = test.Remove(test.Length - 1);
                    }

                    //try
                    //{
                    //    test = test.Remove(test.Length - 1);
                    //}
                    //catch
                    //{

                    //}
                }

                if (a.ActionValue == "C")
                {
                    test = "";
                }

                if (a.ActionValue == "=")
                {
                    test = sc.Eval(test).ToString();
                    can  = true;
                }
            }

            this.Screen.Text = test;
        }
Esempio n. 2
0
        bool newCalcStarted; // has the user started a new calculation?

        public MainForm()
        {
            InitializeComponent();

            //   .select keys from Enum.Keys that will be added to the calculator
            //    this is to allow the calculator to be operated from the NumPad
            foreach (Keys k in Enum.GetValues(typeof(Keys)))
            {
                // Console.WriteLine(k.ToString());
                if (k.ToString().Contains("NumPad") ||
                    arOps.Values.Select(t => t.Method.Name).Contains(k.ToString()) ||
                    k.ToString() == "Decimal" ||
                    k.ToString() == "Return" ||
                    k.ToString() == "Back" ||
                    k.ToString() == "Delete")
                {
                    keyDict[k.ToString()] = k;
                }
            }
            newCalcStarted = true;
            previousValue  = 0;
            List <CalcButton> calcBtns = new List <CalcButton>();

            // create rounded corners of the calculator window
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));

            // populate the numeric key panel with buttons
            int counter = 1;

            for (int row = 0; row < tlpNumericKeys.RowCount; row++)
            {
                for (int column = 0; column < tlpNumericKeys.ColumnCount; column++)
                {
                    string     keyVal  = (counter % 10).ToString();
                    string     keyName = "NumPad" + keyVal;
                    Keys       btnKey  = keyDict[keyName];
                    CalcButton btn     = new CalcButton(btnKey);
                    keysToButtons[btnKey] = btn;
                    tlpNumericKeys.Controls.Add(btn, column, row);
                    btn.Text = keyVal;
                    calcBtns.Add(btn);
                    counter++;
                    if (counter > 10)
                    {
                        break;
                    }
                }
            }

            // add callback functions to numeric keys
            foreach (CalcButton c in calcBtns)
            {
                c.Click += (s, e) =>
                {
                    if (newCalcStarted)
                    {
                        lbCalcHistory.Text = "";
                        tbOutput.Text      = c.Text;
                        newCalcStarted     = false;
                    }
                    else
                    {
                        if (tbOutput.Text == "0")
                        {
                            tbOutput.Text = c.Text;
                        }
                        else
                        {
                            tempString = tbOutput.Text + c.Text;
                            if (Double.TryParse(tempString, out currentValue))
                            {
                                //tbOutput.Text = currentValue.ToString();
                                tbOutput.Text = tempString;
                            }
                        }
                    }
                };
            }

            // add decimal point button and create callback
            CalcButton dotBtn = new CalcButton(keyDict["Decimal"]);

            keysToButtons[keyDict["Decimal"]] = dotBtn;
            tlpNumericKeys.Controls.Add(dotBtn, 1, 3);
            dotBtn.Text = ".";
            //calcBtns.Add(dotBtn);
            dotBtn.Click += (s, e) =>
            {
                if (newCalcStarted)
                {
                    tbOutput.Text      = "0.";
                    lbCalcHistory.Text = "";
                    newCalcStarted     = false;
                }
                else if (!tbOutput.Text.Contains("."))
                {
                    tbOutput.Text += ".";
                }
            };

            // add backspace button and create callback
            CalcButton erase = new CalcButton(keyDict["Back"]);

            keysToButtons[keyDict["Back"]] = erase;
            tlpNumericKeys.Controls.Add(erase, 2, 3);
            erase.Text = "\u2190";
            //calcBtns.Add(dotBtn);
            erase.Click += (s, e) =>
            {
                if (Double.TryParse(tbOutput.Text, out currentValue) &&
                    (Double.IsInfinity(currentValue) || Double.IsNaN(currentValue)))
                {
                    lbCalcHistory.Text = "";
                    tbOutput.Text      = "0";
                }
                else
                {
                    if (tbOutput.Text.Length == 1)
                    {
                        tbOutput.Text = "0";
                    }
                    else
                    {
                        tbOutput.Text = tbOutput.Text.Substring(0, tbOutput.Text.Length - 1);
                        if (newCalcStarted)
                        {
                            lbCalcHistory.Text = "";
                            newCalcStarted     = false;
                        }
                    }
                }
            };

            AddArithmeticFunctions();
            AddAdvancedFunctions();

            // add event handler to each button to handle key presses
            foreach (Control c in this.Controls)
            {
                c.KeyDown += new KeyEventHandler(keypressed);
                foreach (Control cc in c.Controls)
                {
                    cc.KeyDown += new KeyEventHandler(keypressed);
                }
            }
        }
Esempio n. 3
0
        // Add arithmetical functions, erase all and equals buttons and corresponding callbacks
        private void AddArithmeticFunctions()
        {
            CalcButton eraseAll = new CalcButton();

            eraseAll.key = keyDict["Delete"];
            keysToButtons[keyDict["Delete"]] = eraseAll;
            tlpArithmetics.Controls.Add(eraseAll, 0, 0);
            eraseAll.Text   = "CE";
            eraseAll.Click += (sender, e) =>
            {
                currentFunc        = null;
                previousValue      = 0;
                currentValue       = 0;
                lbCalcHistory.Text = "";
                newCalcStarted     = true;
                tbOutput.Text      = "0";
            };

            int rowIdx = 1;

            foreach (string s in arOpsList)
            {
                FuncButton btn = new FuncButton(arOps[s]);
                if (keyDict.Keys.Contains(btn.func.Method.Name))
                {
                    Keys k = keyDict[btn.func.Method.Name];
                    keysToButtons[k] = btn;
                }
                tlpArithmetics.Controls.Add(btn, 0, rowIdx);
                btn.Text = s;
                rowIdx++;
                // The minus button is to be handled differently from the others to allow entry
                // of negative numbers
                if (s == "-")
                {
                    btn.Click += (sender, e) =>
                    {
                        // user entering negative number
                        if (tbOutput.Text == "0" || newCalcStarted)
                        {
                            if (newCalcStarted)
                            {
                                lbCalcHistory.Text = "";
                            }
                            tbOutput.Text  = "-";
                            newCalcStarted = false;
                        }
                        else if (Double.TryParse(tbOutput.Text, out currentValue))
                        {
                            //if (!(Double.IsNaN(currentValue))) performOp(s, btn);
                            setCurrentFunc(s, btn);
                        }
                    };
                }
                else
                {
                    btn.Click += (sender, e) =>
                    {
                        if (Double.TryParse(tbOutput.Text, out currentValue))
                        {
                            //if (!(Double.IsNaN(currentValue))) performOp(s, btn);
                            setCurrentFunc(s, btn);
                        }
                    };
                }
            }
            CalcButton equals = new CalcButton();

            tlpArithmetics.Controls.Add(equals, 0, rowIdx);
            equals.key = keyDict["Return"];
            keysToButtons[keyDict["Return"]] = equals;
            equals.Text   = "=";
            equals.Click += (sender, e) =>
            {
                if (currentFunc != null)
                {
                    if (Double.TryParse(tbOutput.Text, out currentValue))
                    {
                        try
                        {
                            double result = (double)currentFunc.DynamicInvoke(previousValue, currentValue);
                            lbCalcHistory.Text += (" " + tbOutput.Text);
                            tbOutput.Text       = result.ToString();
                            previousValue       = result;
                            currentFunc         = null;
                        }
                        catch (TargetInvocationException ex)
                        {
                            if (ex.InnerException is DivideByZeroException)
                            {
                                lbCalcHistory.Text += (" " + tbOutput.Text);
                                tbOutput.Text       = "INF";
                                previousValue       = 0;
                                currentFunc         = null;
                            }
                        }
                        newCalcStarted = true;
                    }
                }
            };
        }