Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何才能在工作计算器中仅在键盘上使用键。添加_C# - Fatal编程技术网

C# 如何才能在工作计算器中仅在键盘上使用键。添加

C# 如何才能在工作计算器中仅在键盘上使用键。添加,c#,C#,我想做一个只使用键盘的计算器。 如果我在textbox1上按(+、-、/),我想对组合框做出反应 但问题是,当我在键盘上按(+、-、/)时,它不起作用。 我怎样才能解决这个问题 private void button1_Click(object sender, EventArgs e) { if (comboBox1.SelectedItem.ToString() == "+") { int tmp = int.Parse(text

我想做一个只使用键盘的计算器。 如果我在textbox1上按(+、-、/),我想对组合框做出反应 但问题是,当我在键盘上按(+、-、/)时,它不起作用。 我怎样才能解决这个问题

private void button1_Click(object sender, EventArgs e)
    {
        if (comboBox1.SelectedItem.ToString() == "+")
        {
            int tmp = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
            textBox3.Text = tmp.ToString();
        }
        if (comboBox1.SelectedItem.ToString() == "-")
        {
            int tmp = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
            textBox3.Text = tmp.ToString();
        }
        if (comboBox1.SelectedItem.ToString() == "*")
        {
            int tmp = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
            textBox3.Text = tmp.ToString();
        }
        if (comboBox1.SelectedItem.ToString() == "/")
        {
            int tmp = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
            textBox3.Text = tmp.ToString();
        }

    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        textBox1.Focus();
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e .KeyCode == Keys.Add )
        {
            comboBox1.Text = "+";
            textBox2.Focus();
            textBox2.SelectAll();
        }
        if (e .KeyCode == Keys.Subtract )
        {
            comboBox1.Text = "-";
            textBox2.Focus();
            textBox2.SelectAll();
        }

        if (e .KeyCode == Keys.Multiply )
        {
            comboBox1.Text = "*";
            textBox2.Focus();
            textBox2.SelectAll();
        }

        if (e .KeyCode == Keys.Divide )
        {
            comboBox1.Text = "/";
            textBox2.Focus();
            textBox2.SelectAll();
        }



    }

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e .KeyCode == Keys.Enter )
        {
            this.button1_Click(sender, e);
            textBox1.Focus();
            textBox1.SelectAll();
        }
    }
}
我只想在键盘上工作,当我在textbox1上按(+、-、*、/)时,焦点需要转到textbox2和更改组合框


仅当您从键盘的numpad提示
+,-,*,/
时,您的代码才起作用。 将
textBox1_键向下更改为
textBox1_键按下
事件。这使您能够从键盘检查输入字符(
+,-,*,/
):


尝试此操作:您是否尝试使用调试器查看发生了什么情况?请参阅有关使焦点更改生效的说明:&请参阅此处的示例,了解如何处理KeyDown和KeyPress事件以停止将字符输入TextBox:谢谢。我发现了另一个问题。Keys.Add->Keys.Oemplus/Keys.Subtract->Keys.OemMinus可以工作。我不知道为什么……它起作用了!谢谢。我想知道为什么(+、-、*、/)这段代码在我的键盘上只能用numpad,因为键。在键盘和键的numpad上添加+符号检查。Oemplas检查键盘上的另一个+符号。但是如果你像我一样检查输入字符,输入从哪里来并不重要。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '+')
    {
        comboBox1.Text = "+";
        textBox2.Focus();
        textBox2.SelectAll();
    }
    else if (e.KeyChar == '-')
    {
        comboBox1.Text = "-";
        textBox2.Focus();
        textBox2.SelectAll();
    }
    else if (e.KeyChar == '*')
    {
        comboBox1.Text = "*";
        textBox2.Focus();
        textBox2.SelectAll();
    }
    else if (e.KeyChar == '/')
    {
        comboBox1.Text = "/";
        textBox2.Focus();
        textBox2.SelectAll();
    }
}