从键盘上读

时间:2012-10-24 13:02:33

标签: c# focus keypress

我在C#中制作了一个带有两个文本框和按钮的复杂计算器。我想只用键盘操作计算器。我在textbox1_Keypresstextbox2_KeypressForm_Keypress中添加了相同的代码。

  1. 如果焦点在任何文本框中,代码效果很好,但它会在文本框中写入算术符号,我必须手动擦除它以输入另一个值。如何让它不显示在文本框中?
  2. 虽然我在Form_keypress中也使用了相同的代码,但只要我不在文本框中,代码就无法使用。
  3. 关于如何在任何时间立即为键盘做出表单响应的任何建议,无论焦点在哪里?

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

1 个答案:

答案 0 :(得分:0)

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

对于Form1_KeyPress方法,并不是很清楚“无法正常工作”的含义。我想发生的事情是代码永远不会被执行。这是因为KeyPress事件总是发送到表单上的一个控件(我猜你没有2个TextBox)。这意味着如果您想在焦点所在的位置进行相同的操作,则必须注册表单上每个控件的KeyPress事件。

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