码迷,mamicode.com
首页 > Windows程序 > 详细

winform 计算器

时间:2016-12-09 07:34:11      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:cmd   substring   window   blocks   点击   com   and   otto   replace   

技术分享

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 计算器1
{
    public partial class Form1 : Form
    {
        //存储上次点击了什么按钮,0代表什么都没点击,1代表点击了数字按钮,2代表点击了运算符
        private int prev = 0;
        //存储计算的中间结果
        private decimal zj = 0;
        //记录上次按的什么运算符
        private string prevysf = "+";
        public Form1()
        {
            InitializeComponent();
        }
        //数字按钮
        private void button15_Click(object sender, EventArgs e)
        {
            //将事件源转换为按钮
            Button btn = sender as Button;
            //替换(如果下面文本框内容为0或者上次点击了运算符)
            if (prev == 2 || txtbottom.Text == "0")
            {
                txtbottom.Text = btn.Text;
            }
            //追加(如果下面文本框内容不为0并且上次没有点击运算符)
            else
            {
                txtbottom.Text += btn.Text;
            }
            //点击了数字按钮
            prev = 1;
        }
        //运算符按钮
        private void button16_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            //上次按了数字
            if (prev == 1)
            {
                txttop.Text += txtbottom.Text + btn.Text;
                switch (prevysf)
                {
                    case "+":
                        zj = zj + Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "-":
                        zj = zj - Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "*":
                        zj = zj * Convert.ToDecimal(txtbottom.Text);
                        break;
                    case "/":
                        zj = zj / Convert.ToDecimal(txtbottom.Text);
                        break;
                }
                txtbottom.Text = zj.ToString();
            }
            //上次按了运算符
            else
            {
                string s = txttop.Text;
                s = s.Substring(0, s.Length - 1);
                s = s + btn.Text;
                txttop.Text = s;
            }
            //点击了运算符
            prev = 2;
            //记录下运算符
            prevysf = btn.Text;
        }     
        //清零
        private void button1_Click(object sender, EventArgs e)
        {
            txttop.Text = "";
            txtbottom.Text = "0";
            prev = 0;
            zj = 0;
            prevysf = "+";
        }
        //清零
        private void button2_Click(object sender, EventArgs e)
        {
            txtbottom.Text = "0";
        }
        //等号
        private void button20_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            txttop.Text += txtbottom.Text + btn.Text;

            switch (prevysf)
            {
                case "+":
                    zj = zj + Convert.ToDecimal(txtbottom.Text);
                    break;
                case "-":
                    zj = zj - Convert.ToDecimal(txtbottom.Text);
                    break;
                case "*":
                    zj = zj * Convert.ToDecimal(txtbottom.Text);
                    break;
                case "/":
                    zj = zj / Convert.ToDecimal(txtbottom.Text);
                    break;
            }

            txtbottom.Text = zj.ToString();
            txttop.Text = "";

            zj = 0;
        }
        //小数点
        private void button19_Click(object sender, EventArgs e)
        {
            if(txtbottom.Text.Contains(".")==false)
                txtbottom.Text += ".";
        }
        //退格
        private void button3_Click(object sender, EventArgs e)
        {
            txtbottom.Text = txtbottom.Text.Substring(0,txtbottom.TextLength-1);
        }
        //±号控制
        private void button17_Click(object sender, EventArgs e)
        {
            if(!txtbottom.Text.Contains("-"))
           txtbottom.Text= txtbottom.Text.Insert(0, "-");
           else if (txtbottom.Text.Contains("-"))
                txtbottom.Text = txtbottom.Text.Replace("-","");
        }
        //键盘控制
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // if it is a hotkey, return true; otherwise, return false
            switch (keyData)
            {
                case Keys.NumPad0:
                    //焦点定位到控件button_num_0上,即数字0键上
                    button18.Focus();
                    //执行按钮点击操作
                    button18.PerformClick();
                    return true;
                case Keys.NumPad1:
                    button13.Focus();
                    button13.PerformClick();
                    return true;
                case Keys.NumPad2:
                    button14.Focus();
                    button14.PerformClick();
                    return true;
                case Keys.NumPad3:
                    button15.Focus();
                    button15.PerformClick();
                    return true;
                case Keys.NumPad4:
                    button9.Focus();
                    button9.PerformClick();
                    return true;
                case Keys.NumPad5:
                    button10.Focus();
                    button10.PerformClick();
                    return true;
                case Keys.NumPad6:
                    button11.Focus();
                    button11.PerformClick();
                    return true;
                case Keys.NumPad7:
                    button5.Focus();
                    button5.PerformClick();
                    return true;
                case Keys.NumPad8:
                    button6.Focus();
                    button6.PerformClick();
                    return true;
                case Keys.NumPad9:
                    button7.Focus();
                    button7.PerformClick();
                    return true;
                case Keys.Back:
                    button3.Focus();
                    button3.PerformClick();
                    return true;
                case Keys.Divide:
                    button4.Focus();
                    button4.PerformClick();
                    return true;
                case Keys.Multiply:
                    button8.Focus();
                    button8.PerformClick();
                    return true;
                case Keys.Subtract:
                    button12.Focus();
                    button12.PerformClick();
                    return true;
                case Keys.Add:
                    button16.Focus();
                    button16.PerformClick();
                    return true;
                case Keys.Enter:
                    button20.Focus();
                    button20.PerformClick();
                    return true;
                case Keys.Delete:
                    button19.Focus();
                    button19.PerformClick();
                    return true;
                //......
                default:
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }     

    }
}
View Code

 

winform 计算器

标签:cmd   substring   window   blocks   点击   com   and   otto   replace   

原文地址:http://www.cnblogs.com/shadow-wolf/p/6147820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!