本文介绍了如何在计算器中按ON时更换0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个计算器,它运行正常,但是有一个电源按钮,其文本在单击按钮时被更改。首先它是ON,并且在结果文本框中写入0。现在我想用点击按钮取代它,但它没有发生。

请帮帮我。

还有代码便利性:

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;

        }

    }
}



请帮我解决

推荐答案

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();

}


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;
}





谢谢

如果它可以帮助你,请不要忘记标记为解决方案和投票。

[]

Hemant Singh



Thanks
If it helps you, plz dont forget to marks as solution and vote.
C#/Asp.Net Help[^]
Hemant Singh


这篇关于如何在计算器中按ON时更换0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:53