需要计算器的帮助(不按回车键)

本文关键字:回车 计算器 帮助 | 更新日期: 2023-09-27 18:11:11

我想在计算器中使用Enter键作为=,并且我插入了检查用户是否在表单键按中按了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); 
    }

}

代码工作正常,但"Enter Key Condition"不起作用…现在的问题是什么,如何解决?

需要计算器的帮助(不按回车键)

你可以改变这一点:

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 。这将使您能够使用eKeyCode属性,而不必将任何内容强制转换为char

理想情况下,您还应该将switch语句修改为:
switch(e.KeyCode)
{
    case Keys.Back:
        button17.PerformClick();
        break;
    // etcetera
}

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

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

另外,还有一个CancelButton属性,可能用于Esc键的按下。