c# - 将按钮文本属性的值发送到文本框

时间:2016-06-26 12:19:46

标签: c#

我正在制作一个基本计算器,它使用(发送者作为按钮)将相关按钮的文本属性发送到我正在使用的文本框中作为名为' calcScreen'的显示。这意味着如果我按下文本值为' 1'它应该填充文本框。构建运行正常但是当我按下我的数字按钮时,文本框仍然是空白的。我是否错过了Visual Studio中的某个设置或代码是错误的?

using System;
using System.Windows.Forms;  

namespace Calc
{
    public partial class Form1 : Form
    {
        private double accumulator = 0;
        private char lastOperaton;

        public Form1()
        {
            InitializeComponent();
        }

        private void OperatorPressed(object sender, EventArgs e)
        {
            char operation = (sender as Button).Text[0];
            if (operation == 'C')
            {
                accumulator = 0;
            }
            else
            {
                double currentValue = double.Parse(calcScreen.Text);
                switch (lastOperaton)
                {
                    case '+': accumulator += currentValue; break;
                    case '-': accumulator -= currentValue; break;
                    case '*': accumulator *= currentValue; break;
                    case '/': accumulator /= currentValue; break;
                    default: accumulator = currentValue; break;
                }
            }
            lastOperaton = operation;
            calcScreen.Text = operation == '=' ? accumulator.ToString() : "0";
        }

        private void NumberPressed(object sender, EventArgs e)
        {
            string number = (sender as Button).Text;
            calcScreen.Text = calcScreen.Text == "0" ? number : calcScreen.Text + number;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

是的,在我进入visual studio的'Events'并将click事件链接到NumberPressed后,你才对。谢谢你的帮助。