求问一个很简单的问题

.Net技术 码拜 8年前 (2016-03-08) 803次浏览
本人想用c#写一个计算器,首先要做的就是把每一个数字都能输入到文本框中,例如下面这行是代表1这个数字的文本框,
textbox.text = “1”;
可是除了这个,本人还需要在哪里写什么代码才能保证点击1这个按钮后,“1”这个数字出现在文本框中?求问一个很简单的问题
解决方案

15

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Study
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int opMain = 0;        // 运算类型,其中1(加法)  2(减法)  3(乘法)  4(除法) 
        private double mainNum1 = 0;    // 存储第一个数 
        private double mainNum2 = 0;    // 存储第二个数 
        private bool isSecond = false;  // 用来判断输入的是第一个还是第二个数 
        private bool isDone = false;    // 用来判断能否按了等于按钮 
        private bool isDecimal = false; // 用来判断能否有小数 
        private bool isNokeydown = false;// 用来判断能否没输入第二个数而按了"="号键 
        private bool isKeyupclear = true;//用来判断能否按了clear键,程序开始前默认按了; 
        public void setText(string textest)  //设置文本框的值 
        {
            if (textest.Equals("clear"))
            {
                textBox1.Text = "0.";
                isSecond = false;
                isDone = false;
                isDecimal = false;
                isKeyupclear = true;
            }
            else
            {
                if (isSecond)
                {
                    textBox1.Text = textest;
                    isSecond = false;
                    isDecimal = false;
                }
                else
                {
                    if (isDone)
                    {
                        textBox1.Text = textest;
                        isDone = false;
                    }
                    else
                    {
                        if (isKeyupclear)              //对能否按下clear键的判断 
                        {
                            textBox1.Text = textest;
                            isKeyupclear = false;
                        }
                        else
                            textBox1.Text += textest;
                    }
                }
            }
            btnEqual.Select();  //设置"="号的焦点 
        }
        public void Calc(double num1, double num2, int op)
        {
            double answer = 0;
            switch (op)         //判断所进行的运算 
            {
                case 1:
                    answer = num1 + num2;
                    break;
                case 2:
                    answer = num1 - num2;
                    break;
                case 3:
                    answer = num1 * num2;
                    break;
                case 4:
                    answer = num1 / num2;
                    break;
                case 5:
                    answer = num1 % num2;
                    break;
            }
            setText(answer.ToString());  //显示结果 
        }
        //执行运算 
        private void doEquals()
        {
            if (isNokeydown)            //判断已经输入第二个数后按了"="号键 
            {
                mainNum2 = double.Parse(textBox1.Text);
                setText("clear");
                Calc(mainNum1, mainNum2, opMain);
                isDone = true;
                isNokeydown = false;
            }
        }
        //切换正负 
        private void changeSign()
        {
            double storNum;
            if (textBox1.Text.Length > 0)
            {
                storNum = double.Parse(textBox1.Text);
                storNum *= -1;
                textBox1.Text = storNum.ToString();
            }
            btnEqual.Select();  //设置"="号的焦点 
        }
        //设置运算类型 
        private void setOperator(int operation)
        {
            if (textBox1.Text.Length > 0)
            {
                opMain = operation;
                mainNum1 = double.Parse(textBox1.Text);
                isSecond = true;
                isDone = false;
                isNokeydown = true;
                btnEqual.Select();  //设置"="号的焦点 
            }
        }
        //设置小数点 
        private void setDecimal()
        {
            if (!isDecimal)
            {
                setText(".");
                isDecimal = true;
            }
            btnEqual.Select();  //设置"="号的焦点 
        } 
           //开平方
        private void doSquart()
        {
            double storNum;
            storNum = double.Parse(textBox1.Text);
            if (storNum > 0)
            {
                storNum = Math.Sqrt(storNum);
                textBox1.Text = storNum.ToString();
            }
            else
                textBox1.Text = "负数不能开平方。";
            btnEqual.Select();  //设置"="号的焦点 
        }
     
        //求倒数
        private void doreciprocal()
        {
            double storNum;
            storNum = double.Parse(textBox1.Text);
            if (storNum != 0)
            {
                storNum = 1 / storNum;
                textBox1.Text = storNum.ToString();
            }
            else
                textBox1.Text = "除数不能为零。";
            btnEqual.Select();
        }
      
        private void btn7_Click(object sender, EventArgs e)
        {
            setText("7");
        }
        private void btn8_Click(object sender, EventArgs e)
        {
            setText("8");
        }
        private void btn9_Click(object sender, EventArgs e)
        {
            setText("9");
        }
        private void btn4_Click(object sender, EventArgs e)
        {
            setText("4");
        }
        private void btn5_Click(object sender, EventArgs e)
        {
            setText("5");
        }
        private void btn6_Click(object sender, EventArgs e)
        {
            setText("6");
        }
        private void btn1_Click(object sender, EventArgs e)
        {
            setText("1");
        }
        private void btn2_Click(object sender, EventArgs e)
        {
            setText("2");
        }
        private void btn3_Click(object sender, EventArgs e)
        {
            setText("3");
        }
        private void btn0_Click(object sender, EventArgs e)
        {
            setText("0");
        }
        private void btnPoint_Click(object sender, EventArgs e)
        {
            setDecimal(); 
        }
        private void btnSubtract_Click(object sender, EventArgs e)//正负号交换
        {
            changeSign(); 
        }
        private void btnEqual_Click(object sender, EventArgs e)
        {
            doEquals(); 
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            setOperator(1); 
        }
        private void btnSub_Click(object sender, EventArgs e)
        {
            setOperator(2); 
        }
        private void btnMulti_Click(object sender, EventArgs e)
        {
            setOperator(3);
        }
        private void btnDivide_Click(object sender, EventArgs e)
        {
            setOperator(4);
        }
        private void btnC_Click(object sender, EventArgs e)//清除键
        {
            isSecond = false;
            setText("clear"); 
        }
        private void btnSqrt_Click(object sender, EventArgs e)//开平方
        {
            doSquart();
        }
        private void btnPercentage_Click(object sender, EventArgs e)//求模
        {
            setOperator(5);
        }
        private void btnReciprocal_Click(object sender, EventArgs e)//求倒
        {
            doreciprocal();
        }
        private void btnCE_Click(object sender, EventArgs e)//CE
        {
            isSecond = false;
            setText("clear"); 
        }
        private void btnBackspace_Click(object sender, EventArgs e)//退格键
        {
            string old = textBox1.Text;
            if (old.Length >1&&old!="0.")
                textBox1.Text = old.Substring(0, old.Length - 1);
            else
                textBox1.Text = "0.";
            
        }
    }
}

2

拖一个按钮空间,然后在按钮控件的onclick事件中写:this.texebox1.text=控件.value;

3

在按钮1的Click事件处理函数里面写上代码“TextBox.Text+=“1””;
也就是通过改变文本框(显示框)的Text属性达到点击按钮1后 将1显示在文本框中的目的。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求问一个很简单的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)