| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:编写的计算器程序,如何用Keypress事件实现键盘运算
取消只看楼主 加入收藏
卡拉拉
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2013-3-16
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:0 
编写的计算器程序,如何用Keypress事件实现键盘运算
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtDisplay.Text = "0.";

        }
        private bool ClearDisplay = true;

        string LastInput;
        bool exit_小数点 = false;
        bool DecimalFlag = false;
        private string Operator;
        private double Operand1;
        private double Operand2;
        private double result;
        private System.Windows.Forms.Button btn;
        private void button11_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = "";
        }
        private void handleDigits(object sender, EventArgs e)
        {
            btn = (Button)sender;
            if (ClearDisplay)
            {
                txtDisplay.Text = "";
                ClearDisplay = false;
            }
            txtDisplay.Text += btn.Text;
        }
        private void handleoperator(object sender, EventArgs e)
        {
            btn = (Button)sender;
            Operator = btn.Text;
            Operand1 = System.Convert.ToDouble(txtDisplay.Text);
            txtDisplay.Text = "";
        }

        private void button19_Click(object sender, EventArgs e)
        {
            Operand2 = System.Convert.ToDouble(txtDisplay.Text);
            switch (Operator)
            {
                case "+":
                    result = Operand1 + Operand2;
                    txtDisplay.Text = result.ToString();
                    break;
                case "-":
                    result = Operand1 - Operand2;
                    txtDisplay.Text = result.ToString();
                    break;
                case "*":

                    result = Operand1 * Operand2;
                    txtDisplay.Text = result.ToString();
                    break;
                case "/":
                    if (Operand2 == 0)
                    {
                        txtDisplay.Text = "除数不能为零。";
                    }
                    else
                    {
                        result = Operand1 / Operand2;
                        txtDisplay.Text = result.ToString();
                    }
                    break;
                case "%":
                    result = Operand1 % Operand2;
                    txtDisplay.Text = result.ToString();
                    break;
            }

            ClearDisplay = true;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            result = -Convert.ToDouble(txtDisplay.Text);
            txtDisplay.Text = result.ToString();
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (txtDisplay.Text != "0")
            {
                result = 1.0 / Convert.ToDouble(txtDisplay.Text);
                txtDisplay.Text = result.ToString();
                ClearDisplay = true;
            }
            else
            {
                txtDisplay.Text = "除数不能为零。";
            }
        }

        private void button21_Click(object sender, EventArgs e)
        {
            result = Convert.ToDouble(txtDisplay.Text) * Convert.ToDouble(txtDisplay.Text);
            txtDisplay.Text = result.ToString();
        }
        private void button22_Click(object sender, EventArgs e)
        {
            int i = txtDisplay.Text.Length;
            if (i > 0)
                txtDisplay.Text = txtDisplay.Text.Remove(i - 1);
        }

        private void button23_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = null;
            DecimalFlag = false;
            LastInput = "CE";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + ".";
        }
        private void txtDisplay_KeyPress(object sender, KeyPressEventArgs e)
        {


            //用于只接受键盘数字            
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
            }
            //以下是自定义控件               
            //主要是用于接受退格键和‘.’字符 并且此字符只能有一个            
            if (e.KeyChar == (char)8)
            {
                e.Handled = false;
            }
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf(".") < 0)
            {
                e.Handled = false;
            }

        }
    }
}
   
  
   
   

 



[ 本帖最后由 卡拉拉 于 2013-4-7 13:40 编辑 ]
2013-04-06 14:23
快速回复:编写的计算器程序,如何用Keypress事件实现键盘运算
数据加载中...
 
   



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

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