-2

C#我用两个文本框和按钮制作了一个复杂的计算器。我想只使用键盘来操作计算器。我将相同的代码放入textbox1_Keypress,textbox2_KeypressForm_Keypress.

  1. 如果焦点在任何文本框中,则代码运行良好,但它会在文本框中写入算术符号,我必须手动擦除它才能输入另一个值。如何让它不显示在文本框中?
  2. 虽然我也使用了相同的代码,Form_keypress但每当我离开文本框时,代码就不起作用。

关于如何随时随地为键盘立即做出表单响应的任何建议,无论焦点在哪里?

private void textBox1_TextChanged(object sender, EventArgs e)
    {

        if (textBox1.Text != "")
        {
            double d;

            if (flag == 1)
            {
                Double.TryParse(textBox1.Text, out d);
                getOperand.Real = d;
            }
            else if (flag == 2)
            {
                Double.TryParse(textBox1.Text, out d);
                getOperand.Magnitude = d;
            }

        }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if (textBox2.Text != "")
        {
            double d;
            if (flag == 1)
            {
                Double.TryParse(textBox2.Text, out d);
                getOperand.Imag = d;
            }
            else if (flag == 2)
            {
                Double.TryParse(textBox2.Text, out d);
                getOperand.Angle = d;
            }

        }
    }

 private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {

        if (e.KeyChar == '+')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
        }
        else if (e.KeyChar == '-')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 2;
        }
        else if (e.KeyChar == '*')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
        }
        else if (e.KeyChar == '/')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
        }
        else if (e.KeyChar == '=')
        {
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    operand1 = operand1 / operand2;
                    break;
            }
            if (flag == 1)
            {
                textBox1.Text = string.Format("{0:F2}", operand1.Real.ToString());
                textBox2.Text = string.Format("{0:F2}", operand1.Imag.ToString());
            }
            else if (flag == 2)
            {
                textBox1.Text = string.Format("{0:F2}", operand1.Magnitude.ToString());
                textBox2.Text = string.Format("{0:F2}", operand1.Angle.ToString());
            }

            //   MessageBox.Show(string.Format("{0:D2}", operand1.Real.ToString()));

            //  MessageBox.Show(string.Format("{0:D2}", operand1.Imag.ToString()));

            listBox1.Items.Add(operand1);
        }
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == '+')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
        }
        else if (e.KeyChar == '-')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 2;
        }
        else if (e.KeyChar == '*')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
        }
        else if (e.KeyChar == '/')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
        }
        else if (e.KeyChar == '=')
        {
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    operand1 = operand1 / operand2;
                    break;
            }
            if (flag == 1)
            {
                textBox1.Text = string.Format("{0:F2}", operand1.Real.ToString());
                textBox2.Text = string.Format("{0:F2}", operand1.Imag.ToString());
            }
            else if (flag == 2)
            {
                textBox1.Text = string.Format("{0:F2}", operand1.Magnitude.ToString());
                textBox2.Text = string.Format("{0:F2}", operand1.Angle.ToString());
            }

            //   MessageBox.Show(string.Format("{0:D2}", operand1.Real.ToString()));

            //  MessageBox.Show(string.Format("{0:D2}", operand1.Imag.ToString()));

            listBox1.Items.Add(operand1);
        }

    }

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {

    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == '+')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 1;
        }
        else if (e.KeyChar == '-')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 2;
        }
        else if (e.KeyChar == '*')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 3;
        }
        else if (e.KeyChar == '/')
        {
            operand1.Real = getOperand.Real;
            operand1.Imag = getOperand.Imag;
            flag1 = 4;
        }
        else if (e.KeyChar == '=')
        {
            operand2.Real = getOperand.Real;
            operand2.Imag = getOperand.Imag;

            switch (flag1)
            {
                case 1:
                    operand1 = operand1 + operand2;
                    break;
                case 2: operand1 = operand1 - operand2;
                    break;
                case 3:
                    operand1 = operand1 * operand2;
                    break;
                case 4:
                    operand1 = operand1 / operand2;
                    break;
            }
            if (flag == 1)
            {
                textBox1.Text = string.Format("{0:F2}", operand1.Real.ToString());
                textBox2.Text = string.Format("{0:F2}", operand1.Imag.ToString());
            }
            else if (flag == 2)
            {
                textBox1.Text = string.Format("{0:F2}", operand1.Magnitude.ToString());
                textBox2.Text = string.Format("{0:F2}", operand1.Angle.ToString());
            }

            //   MessageBox.Show(string.Format("{0:D2}", operand1.Real.ToString()));

            //  MessageBox.Show(string.Format("{0:D2}", operand1.Imag.ToString()));

            listBox1.Items.Add(operand1);
        }



    }
}
4

1 回答 1

0

在 KeyPress 事件中,您必须将KeyPressEventArgsHandled的属性设置为 true 以避免通常的事件处理(即避免将“+”附加到您的 TextBox)。

对于 Form1_KeyPress 方法,“不起作用”的确切含义还不是很清楚。我想正在发生的事情是代码永远不会执行。这是因为 KeyPress 事件总是被发送到位于表单上的控件之一(我想你不只有 2 个文本框)。这意味着,如果您希望无论焦点在哪里都具有相同的行为,那么您必须注册到表单上每个控件的 KeyPress 事件。

顺便说一句,我会重构只有一个方法处理 KeyPress 的代码,当您需要根据获得输入的控件做一些稍微不同的事情时检查发送者对象。

于 2012-10-24T13:43:22.020 回答