0

This is a simple calculator program I am trying to make using Windows Forms Application in VS. The UnhandledException appears when I click anywhere except on the calculator buttons. I am fairly new to C# and it seems that a sender button in my function "common_operators" is causing the exception. Also, I want this calculator to have similar memory functionalities as windows 10's built-in calculator. I've searched everywhere but couldn't find a c# calculator implementation that is similar to Win10's built-in calculator has, I have already started but I think there's a better way to implement it. If u need more info, I've uploaded the "designer.cs" file that relates to the form application.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp_Calculator;


namespace WindowsFormsApp_Calculator
{ 
    
    public partial class CalculatorBase : Form
        {
            public double[] arrMemory = new double[5];
            public int indexer = 0;
            double result = 0;
            string asmd_operator = ""; // ASMD - Addition, Subtraction, Multiplication, Division
            bool insert_value = false;
            

        public CalculatorBase()
            {
                InitializeComponent();
            btn_mc.Enabled = false;
            btn_mr.Enabled = false;
            mem_textbox.Text = "There's nothing saved in memory";

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

        private void numbers_zerotonine(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if((box_display.Text == "0") || insert_value)
                box_display.Text = "";
            insert_value = false;
            box_display.Text = box_display.Text + b.Text;
        }

        private void common_operators(object sender, EventArgs e)
        {
            Button b = (Button)sender;

            if (result != 0)
            {
                btn_eql.PerformClick();
                insert_value = true;
                asmd_operator = b.Text;
                subbox_display.Text = result + " " + asmd_operator;
            }
            else 
            {
                asmd_operator = b.Text;
                result = double.Parse(box_display.Text);
                box_display.Text = "";
                subbox_display.Text = System.Convert.ToString(result) + " " + asmd_operator;
            }
     
        }

        private void btn_ce_Click(object sender, EventArgs e)
        {
            box_display.Text = "0";
        }

        private void btn_c_Click(object sender, EventArgs e)
        {
            box_display.Text = "0";
            subbox_display.Text = "";
            result = 0;
        }

        private void btn_eql_Click(object sender, EventArgs e)
        {
            subbox_display.Text = "";
            switch(asmd_operator)
            {
                case "-":
                    box_display.Text = (result - double.Parse(box_display.Text)).ToString();
                    break;
                case "+":
                    box_display.Text = (result + double.Parse(box_display.Text)).ToString();
                    break;
                case "X":
                    box_display.Text = (result * double.Parse(box_display.Text)).ToString();
                    break;
                case "/":
                    box_display.Text = (result / double.Parse(box_display.Text)).ToString();
                    break;
                default:
                    break;
            }
            result = double.Parse(box_display.Text);
            asmd_operator = "";
        }

        private void btn_bs_Click(object sender, EventArgs e)
        {
            if(box_display.Text.Length > 0)
            {
                box_display.Text = box_display.Text.Remove(box_display.Text.Length - 1, 1);
            }
            if(box_display.Text == "")
            {
                box_display.Text = "0";
            }
        }

        private void btn_ms_Click(object sender, EventArgs e)
        {
            if (indexer == 5)
            {
                mem_textbox.Text = "Memory is full. Max limit = 5";
            }
            else
            {
                int hgt = 75;
                mem_textbox.Text = "";
                arrMemory[indexer] = double.Parse(box_display.Text);
                indexer++;
                btn_mc.Enabled = true;
                btn_mr.Enabled = true;

                TextBox mem = new TextBox();
                mem.Multiline = true;
                mem.TextAlign = HorizontalAlignment.Right;
                mem.Width = 275;
                mem.Height = 70;
                mem.Font = new Font(mem.Font.FontFamily, 20);
                mem.Text = box_display.Text;
                mem.Location = new Point(387, hgt); 
                this.Controls.Add(mem);
            }
        }

        private void btn_mc_Click(object sender, EventArgs e)
        {
            foreach (int i in arrMemory)
            {
                arrMemory[i] = 0;
            }
            indexer = 0;
            btn_mr.Enabled = false;
            btn_mc.Enabled = false;
            mem_textbox.Text = "There's nothing saved in memory";
        }

        private void btn_mr_Click(object sender, EventArgs e)
        {
            box_display.Text = arrMemory[indexer].ToString();
        }

        private void btn_mp_Click(object sender, EventArgs e)
        {
            arrMemory[indexer] += double.Parse(box_display.Text);
        }

        private void btn_mm_Click(object sender, EventArgs e)
        {
            arrMemory[indexer] -= double.Parse(box_display.Text);
        }


    }
}
2
  • What controls have events subscribed to common_operators? Is one of them not a Button? Commented Mar 3, 2021 at 17:25
  • I've uploaded a link in the desc if u need more info. Commented Mar 3, 2021 at 17:35

1 Answer 1

1

From your designer.cs you've got a Click event handler on the form itself that invokes common_operators, so if that gets fired, it will be an invalid cast since sender will be your CalculatorBase form type and not Button

Sign up to request clarification or add additional context in comments.

2 Comments

And how do I prevent that, sir?
The easiest option is to remove the line in the designer.cs file that reads: this.Click += new System.EventHandler(this.common_operators);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.