标题:编写的计算器程序,如何用Keypress事件实现键盘运算
只看楼主
卡拉拉
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2013-3-16
结帖率:50%
已结贴  问题点数:20 回复次数:1 
编写的计算器程序,如何用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
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:20 
跟你用按扭点击的时候没什么区别啊,只是需要再判断一下键盘输入的运算符,然后该怎么算就怎么算
你不是用Operator来接收运算符的嘛,那你判断键盘输入的如果是运算符,那就给Operator赋值
第一个数字是赋给Operand1吧,第二个数字是赋给Operand2吧
如果输入的是等号什么的,调用 button19_Click方法不就得出结果了
2013-04-08 09:27



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




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

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