需要帮助计算器(不带输入键)

时间:2014-07-03 06:48:45

标签: c#

我想在计算器中使用Enter键as =我插入的代码用于检查用户是否按下了表单键中的Enter键,但此if语句中的代码从未进行过评估 这是我的代码:

    // perform input form keys or buttons
    private void button_click(object sender, EventArgs e)
    {
        if (result.Text == "0" || operation_pressed)
            result.Clear();                    // used to clear textbox 

        operation_pressed = false;
        Button btn = (Button)sender;           // store argument value enterd by user 
        if (btn.Text == ".")
        {
            if (!result.Text.Contains("."))
                result.Text = result.Text + btn.Text;

        }
        else
        result.Text = result.Text + btn.Text;
    }

    // perform CE operation
    private void button19_Click(object sender, EventArgs e)
    {
        result.Text = "0";
        label1.Text = "  ";
        value = 0;
    }

    // perform operator click event when user press any operational key 
    private void operation_click(object sender, EventArgs e)
    {
         Button bt = (Button)sender;
        if(value != 0)
        {
            if (bt.Text == "√")
            {
                result.Text = Math.Sqrt(Double.Parse(result.Text)).ToString();
                value = Math.Sqrt(Double.Parse(result.Text));
            }
            equal.PerformClick();
            operation_pressed = true;
            operation = bt.Text;
            label1.Text = value + " " + operation;
        }
        else if (bt.Text == "√")
        {
            result.Text = Math.Sqrt(Double.Parse(result.Text)).ToString();
            value = Math.Sqrt(Double.Parse(result.Text));
        }
        else
        {

            operation = bt.Text;
            value = double.Parse(result.Text);
            operation_pressed = true;
            label1.Text = value + " " + operation;
        }

    }

    // perform actual operations on values, and show result on textbox
    private void equal_click(object sender, EventArgs e)
    {
        label1.Text = " ";

        switch (operation)
        {
            case "+":
                result.Text = (value + double.Parse(result.Text)).ToString();
                break;

            case "-":
                result.Text = (value - double.Parse(result.Text)).ToString();
                break;

            case "x":
            case "*":
                result.Text = (value * double.Parse(result.Text)).ToString();
                break;

            case "/":
                result.Text = (value / double.Parse(result.Text)).ToString();
                break;


            default:
                break;

        }
        value = int.Parse(result.Text);
        operation = " ";
    }

    // this method is used to take input from keyboard 
    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {

        switch (e.KeyChar.ToString())
        { 
            case "0":
                zero.PerformClick();
                break;
            case "1":
                one.PerformClick();
                break;
            case "2":
                two.PerformClick();
                break;
            case "3":
                three.PerformClick();
                break;
            case "4":
                four.PerformClick();
                break;
            case "5":
                five.PerformClick();
                break;
            case "6":
                six.PerformClick();
                break;
            case "7":
                seven.PerformClick();
                break;
            case "8":
                eight.PerformClick();
                break;
            case "9":
                nine.PerformClick();
                break;
            case "+":
                add.PerformClick();
                break;
            case "-":
                sub.PerformClick();
                break;
            case "*":
                mul.PerformClick();
                break;
            case "/":
                div.PerformClick();
                break;
            case "=":
                equal.PerformClick();
                break;

            default:
                break;

        } //end switch 

        if (e.KeyChar == (char)Keys.Enter)
            equal.PerformClick();
        if (e.KeyChar == (char)Keys.Back)
            button17.PerformClick();

    }

    // used to delete last insert character from textbox
    private void button17_Click(object sender, EventArgs e)
    {
        string sample = result.Text;
        int a = sample.Length - 1;
        result.Text = sample.Remove(a); 
    }


}

代码工作正常,但“输入密钥条件”不起作用... 现在问题是什么,如何解决?

2 个答案:

答案 0 :(得分:1)

您可以更改此位:

if (e.KeyChar == (char)Keys.Enter)
    equal.PerformClick();
if (e.KeyChar == (char)Keys.Back)
    button17.PerformClick();

要:

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    // ... your other code

    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
        equal.PerformClick();
    if (e.KeyCode == Keys.Back)
        button17.PerformClick();
}

请注意,我使用的是表单的KeyUp事件,而不是KeyPress 。这样,您就可以使用KeyCode的{​​{1}}属性,而且不必向e投射任何内容。

理想情况下,您还应将switch语句修改为:

char

答案 1 :(得分:0)

您可以使用System.Windows.Forms.Form类的AcceptButton属性,如下所示:

//when Enter key is pressed anywhere on form, yourEqualButton's Click event is fired.
yourForm.AcceptButton = yourEqualButton;

此外,还有一个CancelButton属性可能会用于Esc按键。