Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
I have made a calculator, it is working fine but there is a power button whose text is changed on clicking the button.First it is ON and 0 is written in the result text box. Now i want to replace it with the value on the click of the button, but it is not happening.
Please help me out with this.
There is code also for the convenience as:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace Calculator_full
{
    public partial class calculator : System.Web.UI.Page
    {
        
        Decimal Num1;
        Decimal Num2;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
 
                btn_power.Text = "ON";
 
                btn_power.BackColor = Color.Green;
 
                btn_0.Enabled = false;
                btn_1.Enabled = false;
                btn_2.Enabled = false;
                btn_3.Enabled = false;
                btn_4.Enabled = false;
                btn_5.Enabled = false;
                btn_6.Enabled = false;
                btn_7.Enabled = false;
                btn_8.Enabled = false;
                btn_9.Enabled = false;
                btn_clear.Enabled = false;
                btn_clearall.Enabled = false;
                btn_multiply.Enabled = false;
                btn_divide.Enabled = false;
                btn_equal.Enabled = false;
                btn_add.Enabled = false;
                btn_sub.Enabled = false;
 
             }
        }
 

 
        protected void btn_1_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + 1;
           
        }
 
        protected void btn_8_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_8.Text;
        }
 
        protected void btn_2_Click(object sender, EventArgs e)
        {
            //txt_result.Text = txt_result.Text + btn_2.Text;
            txt_result.Text = txt_result.Text + 2;
        }
 
        protected void btn_3_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_3.Text;
        }
 
        protected void btn_4_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_4.Text;
        }
 
        protected void btn_5_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_5.Text;
        }
 
        protected void btn_6_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_6.Text;
        }
 
        protected void btn_7_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_7.Text;
        }
 
        protected void btn_9_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_9.Text;
        }
 
        protected void btn_0_Click(object sender, EventArgs e)
        {
            txt_result.Text = txt_result.Text + btn_0.Text;
        }
 
        protected void btn_add_Click(object sender, EventArgs e)
        {
            Num1 = Convert.ToDecimal(txt_result.Text);
            Session["first"] = Num1;
            Session["Func"] = "ADD";
            txt_result.Text = string.Empty;
        }
 
        protected void btn_equal_Click(object sender, EventArgs e)
        {
            Num2 = Convert.ToDecimal(txt_result.Text);
            if (Session["Func"].ToString() == "ADD")
            {
                decimal res = 0;
                Convert.ToString(Convert.ToDecimal(Session["First"]) + Num2);
                res = Convert.ToDecimal(Session["First"]) + Num2;
                txt_result.Text = String.Format("{0:0.########}", res);
            }
            else if (Session["Func"].ToString() == "SUB")
            {
                decimal res = 0;
                res = Convert.ToDecimal(Session["First"]) - Num2;
                txt_result.Text = String.Format("{0:0.########}", res);
            }
            else if (Session["Func"].ToString() == "DIV")
            {
                Convert.ToString(Convert.ToDecimal(Session["First"]) / Num2);
                decimal res = 0;
                res = Convert.ToDecimal(Session["First"]) / Num2;
                txt_result.Text = String.Format("{0:0.########}", res);
            }
            else if (Session["Func"].ToString() == "MUL")
            {
                Convert.ToString(Convert.ToDecimal(Session["First"]) * Num2);
                decimal res = 0;
                res = Convert.ToDecimal(Session["First"]) * Num2;
                txt_result.Text = String.Format("{0:0.########}", res);
 
            }
 
        }
 

 
        protected void btn_divide_Click(object sender, EventArgs e)
        {
            Num1 = Convert.ToDecimal(txt_result.Text);
            Session["first"] = Num1;
            Session["Func"] = "DIV";
            txt_result.Text = string.Empty;
        }
 
        protected void btn_multiply_Click(object sender, EventArgs e)
        {
            Num1 = Convert.ToDecimal(txt_result.Text);
            Session["first"] = Num1;
            Session["Func"] = "MUL";
            txt_result.Text = string.Empty;
        }
 
        protected void btn_sub_Click(object sender, EventArgs e)
        {
            Num1 = Convert.ToDecimal(txt_result.Text);
            Session["first"] = Num1;
            Session["Func"] = "SUB";
            txt_result.Text = string.Empty;
        }
 
        protected void btn_clear_Click(object sender, EventArgs e)
        {
            clearall();
        }
 
        protected void btn_clearall_Click(object sender, EventArgs e)
        {
            clearall();
        }
 
        protected void btn_power_Click(object sender, EventArgs e)
        {
            btn_power.Text = "OFF";
            btn_power.BackColor = Color.Red;
            btn_0.Enabled = true;
            btn_1.Enabled = true;
            btn_2.Enabled = true;
            btn_3.Enabled = true;
            btn_4.Enabled = true;
            btn_5.Enabled = true;
            btn_6.Enabled = true;
            btn_7.Enabled = true;
            btn_8.Enabled = true;
            btn_9.Enabled = true;
            btn_clear.Enabled = true;
            btn_clearall.Enabled = true;
            btn_multiply.Enabled = true;
            btn_divide.Enabled = true;
            btn_equal.Enabled = true;
            btn_add.Enabled = true;
            btn_sub.Enabled = true;
 
            txt_result.Text = "0";
            Session.Abandon();
 
        }
 
        public void clearall()
        {
            txt_result.Text = "";
        }
 
        protected void txt_result_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        protected void btn_decimal_Click(object sender, EventArgs e)
       
        {
            txt_result.Text = txt_result.Text + btn_decimal.Text;
 
        }
       
    }
}
Please help me out as it is urgent.
Posted 31-Jan-13 0:59am
Edited 31-Jan-13 2:02am
v3
Comments
Mike Meinz at 31-Jan-13 7:44am
   
Please clarify what you mean.
 
You say Now I want to replace it with the value on the click of the button
 
What is "it"?
What is "the value"?
What is "the button"?
ryanb31 at 31-Jan-13 7:54am
   
Your description sounds very easy but there is lots of code. Can you simplify down what the problem is?
CodeMaster_Noob at 31-Jan-13 8:08am
   
can you provide .aspx page code also..??
Ravinder Kumar Sharma at 1-Feb-13 2:35am
   
Thanx, I am sending the aspx code as well as under:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="calculator.aspx.cs" Inherits="Calculator_full.calculator" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Calculator.css" rel="stylesheet" type="text/css" />

<style type="text/css">
.style2
{
margin-left: 4px;
}
.style3
{
margin-left: 0px;
}
.style4
{
background-color: Olive;
font-family: Arial;
font-size: 15px;
font-weight: bold;
color: Black;
}
.style5
{
background-color: red;
font-family: Arial;
font-size: 15px;
font-weight: bold;
color: Black;
}
.style6
{
background-color: Olive;
font-family: Arial;
font-size: 15px;
font-weight: bold;
color: Black;
margin-left: 0px;
}
.style7
{
background-color: gray;
border: solid 2px black;
font-family: Arial;
font-weight: bold;
color: black;
font-size: 16px;
vertical-align: middle;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">

<asp:UpdatePanel ID="UpdatePanel1" runat="server">


<table align="center" width="35%" style="border-left: 2px solid #00F; border-right: 2px solid #00F;
border-bottom: 2px solid #00F; border-top: 2px solid #00F;">
<tr>
<td colspan="6">
 
</td>
</tr>
<tr>
<td colspan="6" align="center">
<asp:TextBox ID="txt_result" runat="server" Style="text-align: right" Width="93%"
Height="30px" BorderColor="Black" BorderStyle="Groove" CssClass="style3" OnTextChanged="txt_result_TextChanged">
</td>
</tr>
<tr>
<td colspan="6">
 
</td>
</tr>
<tr>
<td colspan="2" width="33.33%" align="center">
<asp:Button ID="btn_clear" runat="server" CssClass="style6" Text="C" BorderColor="Black"
Font-Bold="True" Width="104px" OnClick="btn_clear_Click" />
</td>
<td colspan="2" width="33.33%" align="center">
<asp:Button ID="btn_clearall" runat="server" Text="CE" CssClass="style4" BorderColor="Black"
Font-Bold="True" Width="102px" OnClick="btn_clearall_Click" />
</td>
<td colspan="2" width="33.33%" align="center">
<asp:Button ID="btn_power" runat="server" Text="ON/OFF" CssClass="style5" BorderColor="Black"
Font-Bold="True" Width="108px" OnClick="btn_power_Click" />
</td>
</tr>
<tr>
<%--<td width = "33.33%" class="style1">
<asp:Button ID="btn_clear" runat="server" Text="C" BorderColor="Black" Font-Bold="True"
Width="55px" />
</td>

<td width="33.33%">
<asp:Button ID="btn_clearall" runat="server" Text="CE" BorderColor="Black" Font-Bold="True"
Width="60px" />
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Just a few hints here on making your code a little cleaner...
protected void btn_power_Click(object sender, EventArgs e)
{
   bool isPowerTurningOn = (btn_power.Text == "OFF");
 
   // This will act as a toggle switch for the button text
   btn_power.Text = isPowerTurningOn ? "ON" : "OFF";
   btn_power.BackColor = isPowerTurningOn ? Color.Green : Color.Red;
 
   // Instead of calling each item in a separate line,
   // use a loop to make your life easier.
   foreach( var ctl in Controls )
   {
      if( ctl is Button )
      {
         ctl.Enabled = isPowerTurningOn;
      }
   } 
   
   txt_result.Text = isPowerTurningOn ? "0" : string.Empty;
   Session.Abandon();
 
}
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think you need as below code...
 
   public static string str= "";
   public static int count = 0;
   protected void Button1_Click(object sender, EventArgs e)
   {
       count++;
       if (count % 2 == 0)
           str = "On";
       else
           str = "Off";
       Button1.Text = str;
   }
 
Thanks
If it helps you, plz dont forget to marks as solution and vote.
C#/Asp.Net Help[^]
Hemant Singh
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Richard Deeming 135
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,067
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100