2012-10-24 107 views
-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); 
     } 



    } 
} 
+3

請顯示一些相關代碼(即來自'Keypress'處理程序的代碼)。你做過任何研究嗎?你看過['KeyPreview'](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx)屬性嗎? – CodeCaster

+0

我將KeyPreview設置爲true。 – joseph

+1

我只是將代碼添加到問題中 – joseph

回答

0

在KeyPress事件,你必須的KeyPressEventArgsHandled屬性設置爲true 避免了通常事件處理(即以避免「+」添加到您的文本框)。

對於Form1_KeyPress方法,並不十分清楚「準確無效」究竟意味着什麼。我想現在發生的事情是代碼永遠不會被執行。這是因爲KeyPress事件總是發送到位於窗體上的控件之一(我沒有,只有2個文本框)。這意味着如果您想在焦點所在的位置使用相同的行爲,則必須註冊到表單上每個控件的KeyPress事件。

順便說一句我會重構代碼只有一個單一的方法處理KeyPress,檢查發件人對象,當你需要做一些不同的取決於獲得輸入的控制。