簡體   English   中英

使用按鍵時結果翻倍

[英]Double result when using keypress

我用C#制作了這個計算器,但遇到一個問題:當我按鍵盤上的數字1時,會給我錯誤的結果雙精度數。 我沒有得到正確的結果。

你知道我在做什么錯嗎

非常感謝你!

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;
        }
    }
}

我將其從按鍵更改為KeyUp。 這樣,只有在釋放鑰匙后才會觸發。

編輯:簽出此信息: Visual Studio中的KeyDown事件,KeyPress事件和KeyUp事件之間的區別

你不需要在你的追加keychar result.Text (假設result是你的TextBox項目),你都寫在一行result.Text += e.KeyChar.ToString(); 那條線使您的輸入加倍。

刪除它,它將按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM