标题:请问开始时为什么无法在窗体上操作,必须先用键盘输入后,才能用窗体的按钮
取消只看楼主
水冰
Rank: 1
等 级:新手上路
帖 子:7
专家分:5
注 册:2013-3-30
结帖率:50%
已结贴  问题点数:20 回复次数:0 
请问开始时为什么无法在窗体上操作,必须先用键盘输入后,才能用窗体的按钮
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 计算器的设计
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        double dblnumA = 0;
        double dblnumB = 0;
        //定义操作符
        static string oper = "";
        //定义操作数是否有小数点
        static bool point = false;
        //记录小数点后数字的个数
        int dotnum = 0;
        //用于保存最初的操作数,以便进行连续敲击等号运算
        double dblnum = 0;
        //用于记录是否连续敲击等号按钮
        bool takeequal = true;

        private void display(int i)
        {
            if (point)
            {
                dotnum++;
                if (i == 0)
                {
                    this.textBox1.Text = this.textBox1.Text + "0";
                }
                if (i != 0)
                {
                    this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) + i / (Math.Pow(10, dotnum)));
                }
            }
            else
            {
                this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
                this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + i);
                this.textBox1.Text = this.textBox1.Text + ".";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            display(0);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            display(1);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            display(2);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            display(3);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            display(4);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            display(5);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            display(6);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            display(7);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            display(8);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            display(9);
        }
        private void operate(string sign)
        {
            dblnumA = double.Parse(this.textBox1.Text);
            point = false;
            this.textBox1.Text = "0.";
            oper = sign;
            takeequal = true;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            operate("/");
        }

        private void button20_Click(object sender, EventArgs e)
        {
            operate("*");
        }

        private void button21_Click(object sender, EventArgs e)
        {
            operate("-");
        }

        private void button22_Click(object sender, EventArgs e)
        {
            operate("+");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            operate("%");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = Convert.ToString(double.Parse(this.textBox1.Text) *(-1));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (double.Parse(this.textBox1.Text) != Convert.ToInt64(double.Parse(this.textBox1.Text))||point)
            {
                this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length);
                point = false;
            }
            else
            {
                this.textBox1.Text = this.textBox1.Text + "";
                this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
            }
            this.textBox1.Text = Convert.ToString(Math.Sqrt(Double.Parse(this.textBox1.Text)));
            takeequal = false;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (takeequal)
            {
                dblnum = dblnumB = double.Parse(this.textBox1.Text);
                if (oper == "+")
                {
                    this.textBox1.Text = Convert.ToString(dblnumA + dblnumB);
                }
                if (oper == "-")
                {

                    this.textBox1.Text = Convert.ToString(dblnumA - dblnumB);
                }
                if (oper == "*")
                {
                    this.textBox1.Text = Convert.ToString(dblnumA * dblnumB);
                }
                if (oper == "/")
                {
                    if (dblnumB == 0)
                    {
                        MessageBox.Show("除数为零,非法操作!");
                    }
                    else
                        this.textBox1.Text = Convert.ToString(dblnumA / dblnumB);
                }
                if (oper == "%")
                {
                    this.textBox1.Text = Convert.ToString(dblnumB % dblnumB);
                }
                else
                    if (double.Parse(this.textBox1.Text) == Convert.ToInt64(double.Parse(this.textBox1.Text))
                        && 0 != double.Parse(this.textBox1.Text))
                    {
                        this.textBox1.Text = this.textBox1.Text + ".";
                    }
            }
            else
                WorkLikeFront();
                takeequal = false;
        }
        //退格键
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
            if (this.textBox1.Text == "")
            {
                this.textBox1.Text = "0.";
            }
        }
        //清除键
        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "0";
            oper = "";
            point = false;
            dotnum = 0;
            dblnumA = 0;
            takeequal = true;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length);
            this.textBox1.Text = Convert.ToString(1 / double.Parse(this.textBox1.Text));
        }

        private void button15_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
            this.textBox1.Text = this.textBox1.Text = ".";
            point = true;
        }
        private void WorkLikeFront()
        {
            double dblNumB2 = double.Parse(this.textBox1.Text);
            if (oper == "+")
            {
                this.textBox1.Text = Convert.ToString(dblNumB2 + dblnum);
            }
            if (oper == "-")
            {
                this.textBox1.Text = Convert.ToString(dblNumB2 + dblnum);
            }
            if (oper == "*")
            {
                this.textBox1.Text = Convert.ToString(dblNumB2 * dblnum);

            }
            if (oper == "/")
            {
                if (dblnum == 0)
                {
                    MessageBox.Show("除数为零,非法操作!");

                }
                else
                    this.textBox1.Text = Convert.ToString(dblNumB2 / dblnum);
            }
            if (oper == "%")
            {
                this.textBox1.Text = Convert.ToString(dblNumB2 % dblnum);
            }
            else
                if (double.Parse(this.textBox1.Text) == Convert.ToInt64(double.Parse(this.textBox1.Text))
                       && 0 != double.Parse(this.textBox1.Text))
                {
                    this.textBox1.Text = this.textBox1.Text + ".";
                }

        }


      
    }
}
这是一个计算器
搜索更多相关主题的帖子: static public double 小数点 
2013-04-10 15:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-403637-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.329773 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved