简体   繁体   English

使用按键时结果翻倍

[英]Double result when using keypress

I made this calculator in C# and I have one problem: When I press keyboard something like 1 it gives me incorrect result double number. 我用C#制作了这个计算器,但遇到一个问题:当我按键盘上的数字1时,会给我错误的结果双精度数。 I don't get the correct result. 我没有得到正确的结果。

Do you know what I am doing wrong, 你知道我在做什么错吗

Thank you very much! 非常感谢你!

namespace Calculator
{
    public partial class Calculator : Form
    {
        Double value = 0;
        String operation = "";
        int i = 0;
        bool operation_pressed = false;
        bool button_dot = false;
        OperationClass class1 = new OperationClass();
        public Calculator()
        {
            InitializeComponent();
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(Calculator_KeyPress);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Calculator_Load(object sender, EventArgs e)
        {

        }

        private void Calculator_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((result.Text == "0") || (operation_pressed))
                result.Clear();
            switch (e.KeyChar)
            {
                // key press from 0-9
                case (char)48:
                case (char)49:
                case (char)50:
                case (char)51:
                case (char)52:
                case (char)53:
                case (char)54:
                case (char)55:
                case (char)56:
                case (char)57:
                    e.Handled = true;
                    result.Text += e.KeyChar.ToString();
                    break;
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            if ((result.Text == "0") || (operation_pressed))
                result.Clear();
            operation_pressed = false;
            Button b = (Button)sender;
            result.Text = result.Text + b.Text;
        }

        private void operator_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operation = b.Text;
            value = Double.Parse(result.Text);
            operation_pressed = true;
            equation.Text = value + " " + operation;
        }

        private void buttonCE_Click(object sender, EventArgs e)
        {
            result.Text = "0";
            equation.Text = "";
            button_dot = false;
        }

        private void buttonC_Click(object sender, EventArgs e)
        {
            result.Clear();
            value = 0;
        }

        private void buttonEqual_Click(object sender, EventArgs e)
        {
            equation.Text = "";
            button_dot = false;
            operation_pressed = true;
            if (operation != "")
                result.Text = class1.GetResult(operation, value, result.Text);
            else
                result.Text = result.Text + "";
        }

        private void buttonDot_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if ((!button_dot && !operation_pressed) || result.Text == "0")
                result.Text = result.Text + b.Text;
            button_dot = true;
        }
    }
}

I'd change it from keypress to KeyUp instead. 我将其从按键更改为KeyUp。 That way it only fires as soon as the key is released. 这样,只有在释放钥匙后才会触发。

Edit: Check out this info: Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio 编辑:签出此信息: Visual Studio中的KeyDown事件,KeyPress事件和KeyUp事件之间的区别

You don't need to append keychar in your result.Text (Assuming result is your TextBox item) as you have written in line result.Text += e.KeyChar.ToString(); 你不需要在你的追加keychar result.Text (假设result是你的TextBox项目),你都写在一行result.Text += e.KeyChar.ToString(); . That's the line which is doubling your input. 那条线使您的输入加倍。

Remove it and it'll work as expected. 删除它,它将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM