Code for the above design (Default.aspx)


    <form id="form1" runat="server">  
    <asp:Button ID="btn2" runat="server" Height="30px"
        Text="2" Width="30px" onclick="btn_Click" />
    <asp:TextBox ID="txtValue" runat="server"
    <asp:Button ID="btn3" runat="server" Height="30px"
        Text="3" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn8" runat="server" Height="30px"
        Text="8" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btnAdd" runat="server" Height="30px"
        Text="+" Width="30px" onclick="btnAdd_Click" />
    <asp:Button ID="btn7" runat="server" Height="30px"
        Text="7" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn6" runat="server" Height="30px"
        Text="6" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn5" runat="server" Height="30px"
        Text="5" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn4" runat="server" Height="30px"
        Text="4" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn1" runat="server" Height="30px"
        Text="1" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn9" runat="server" Height="30px"
        Text="9" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btn0" runat="server" Height="30px"
        Text="0" Width="30px" onclick="btn_Click" />
    <asp:Button ID="btnDiv" runat="server" Height="30px"
        Text="/" Width="30px" onclick="btnDiv_Click" />
    <asp:Button ID="btnMul" runat="server" Height="30px"
        Text="x" Width="30px" onclick="btnMul_Click" />
    <asp:Button ID="btnSub" runat="server" Height="30px"
        Text="-" onclick="btnSub_Click" />
    <asp:Button ID="btnDec" runat="server" Height="30px"
        Text="." Width="30px" onclick="btnDec_Click" />
    <asp:Button ID="btnEqual" runat="server" Height="30px"
        Text="=" Width="30px" onclick="btnEqual_Click" />
    <asp:Button ID="btnClose" runat="server" Text="Close"
    <asp:Button ID="btnClear" runat="server" Text="Clear"
C Sharp code for above design(Default.aspx.cs)


             //declaring globalvalues
             //val for value
   Double val=0;
        enum operators { Add=1, Sub, Mul,Div, Equal };
           //operator_clicked to checked last operator clicked.
        int operator_clicked = 0;
          //pressed to check for number button pressed
        Boolean pressed;
         //equal function which is used with every math operator
        void equal()
             //initilizing value of viewstate in val to get the previous value of val
             //before clicking any button
            val = (double)ViewState["val"];
              //fetching value of operator button last clicked in o
            int o = (int)ViewState["operator"];
              //performing task according to the value of o
            switch (o)
                case (int)operators.Add:
                    val = (val + Convert.ToDouble(txtValue.Text));
                case (int)operators.Sub:
                    val = (val - double.Parse(txtValue.Text));
                case (int)operators.Mul:
                    val = (val * double.Parse(txtValue.Text));
                case (int)operators.Div:
                    val = (val / double.Parse(txtValue.Text));
                    if (txtValue.Text != "")
                        val = Convert.ToDouble(txtValue.Text);
                        val = 0;
//assigning textbox text to the val computed above
            txtValue.Text = val.ToString();
//storing values in view state.
            ViewState["val"] = val;
            ViewState["dec_pressed"] = false;
            ViewState["pressed"] = false;
        protected void Page_Load(object sender, EventArgs e)
         //checking condition whether page is reloaded or not if not reloaded ie. If
           //it is loaded for the first time then the below statements will execute.
            if (!IsPostBack)
                val = 0;
                ViewState["val"] = val;
                ViewState["operator"] = 0;
                ViewState["pressed"] = false;
                ViewState["dec_pressed"] = false;
                txtValue.Text = "0";
            //on number button click event this function will execute.
        protected void btn_Click(object sender, EventArgs e)
            //checking whether number button is pressed first time after clicking any
             //operator or not
            pressed = (Boolean)ViewState["pressed"];
            if (pressed==false)
                txtValue.Text = "";
            ViewState["pressed"] = true;
            //concatinating text of button clicked to the text of text box.
            txtValue.Text += ((Button)sender).Text;
        protected void btnAdd_Click(object sender, EventArgs e)
            //setting operator clicked value to value of operators.add ie.1 and storing
              //in viewstate for further use.
            operator_clicked = (int)operators.Add;                 
            ViewState["operator"] = operator_clicked;          
        protected void btnSub_Click(object sender, EventArgs e)
            operator_clicked = (int)operators.Sub;         
            ViewState["operator"] = operator_clicked;         
        protected void btnMul_Click(object sender, EventArgs e)
            operator_clicked = (int)operators.Mul;          
            ViewState["operator"] = operator_clicked;         
        protected void btnDiv_Click(object sender, EventArgs e)
            operator_clicked = (int)operators.Div;    
            ViewState["operator"] = operator_clicked;        
        protected void btnEqual_Click(object sender, EventArgs e)
           //setting value of val to 0 and storing in viewstate and operator_clicked
             //value to value of equal ie. 5
            val = 0;
            ViewState["val"] = val;
            operator_clicked = (int)operators.Equal;
            ViewState["operator"] = operator_clicked;         
                //this will clear text box and assign initial values to viewstate.
        protected void btnClear_Click(object sender, EventArgs e)
            txtValue.Text = "";
            ViewState["val"] = 0;
            ViewState["operator"]= 0;
            ViewState["pressed"]= false;
            ViewState["dec_pressed"]= false;
        protected void btnClose_Click(object sender, EventArgs e)
              //java script is used to close window.
            Response.Write("<script language='javascript'> {window.close();}</script>");
             //put decimal point in text box if it does not contain any.
        protected void btnDec_Click(object sender, EventArgs e)
            pressed = (Boolean)ViewState["pressed"];
            if (pressed == false)
                txtValue.Text= "";
             //checking whether decimal is ther in text box or not.
            if ((bool)ViewState["dec_pressed"] == false)
                if (txtValue.Text == "")
                    txtValue.Text = "0.";
                    txtValue.Text += ".";
              //setting deciaml pressed view satate and number pressed view state to true.
            ViewState["dec_pressed"] = true;
            ViewState["pressed"] = true;

  Snap shot



  Modified On Nov-29-2017 03:48:21 PM

Leave Comment