2011-12-27 119 views
3

我想要做一個計算器,我所要做的就是使它與鍵盤一起工作。這應該工作,但它不。在c#中的keydown不工作由於某種原因

private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.D1) 
     { 
      resultarea.Text = "fgdgd"; 
      //number(1); 

     } 
    .... 
    } 

任何想法可能出了什麼問題?

編輯:它仍然無法正常工作。我想我可能會發布整個代碼。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     KeyPreview = true; 

    } 
    double first = 0; 
    double second = 0; 
    string op = ""; 
    bool last = true; 

    public void calculate() 
    { 
     double aux = first; 
     first = second; 

     if (op == "+") 
      second = aux + second; 
     else if (op == "-") 
      second = aux - second; 
     else if (op == "*") 
      second = aux * second; 
     else if (op == "/") 
      if (second == 0) 
       { first = aux; viewarea.Text = "Cannot divide by zero"; } 
      else 
       second = aux/second; 
     //if (viewarea.Text == "Cannot divide by zero") 
      resultarea.Text = second.ToString(); 

    } 

    public void number(int digit) 
    { 
     if (last == true) 
     { 
      resultarea.Clear(); 
      last = false; 
      first = second; 
      second = 0; 
     } 
     resultarea.Text += digit.ToString(); 
     second = second * 10 + digit; 

    } 

    public void sign(string sign) 
    { 
     if (last==false && (first != 0 && second != 0)) 
      calculate(); 
     op = sign; 
     debug.Text = op; 
     last = true; 
    } 

    private void equal_Click(object sender, EventArgs e) 
    { 
     if (last == false && (first != 0 && second !=0)) 
      calculate(); 
     op = ""; 
     last = true; 
    } 

    private void one_Click(object sender, EventArgs e) 
    { 
     number(1); 
    } 

    private void two_Click(object sender, EventArgs e) 
    { 
     number(2); 
    } 

    private void plus_Click(object sender, EventArgs e) 
    { 
     sign("+"); 
    } 

    private void minus_Click(object sender, EventArgs e) 
    { 
     sign("-"); 
    } 



    private void three_Click(object sender, EventArgs e) 
    { 
     number(3); 
    } 

    private void creset_Click(object sender, EventArgs e) 
    { 
     first = 0; 
     second = 0; 
     op = ""; 
     last = true; 
     viewarea.Text = ""; 
     resultarea.Text = ""; 
     debug.Text = ""; 
    } 

    private void four_Click(object sender, EventArgs e) 
    { 
     number(4); 
    } 

    private void five_Click(object sender, EventArgs e) 
    { 
     number(5); 
    } 

    private void six_Click(object sender, EventArgs e) 
    { 
     number(6); 
    } 

    private void seven_Click(object sender, EventArgs e) 
    { 
     number(7); 
    } 

    private void eight_Click(object sender, EventArgs e) 
    { 
     number(8); 
    } 

    private void nine_Click(object sender, EventArgs e) 
    { 
     number(9); 
    } 

    private void zero_Click(object sender, EventArgs e) 
    { 
     number(0); 
    } 

    private void divide_Click(object sender, EventArgs e) 
    { 
     sign("/"); 
    } 

    private void times_Click(object sender, EventArgs e) 
    { 
     sign("*"); 
    } 

    private void sqr_Click(object sender, EventArgs e) 
    { 
     second = Math.Sqrt(second); 
     resultarea.Text = second.ToString(); 
    } 

    private void cos_Click(object sender, EventArgs e) 
    { 
     second = Math.Cos(second); 
     resultarea.Text = second.ToString(); 
    } 

    private void sin_Click(object sender, EventArgs e) 
    { 
     second = Math.Sin(second); 
     resultarea.Text = second.ToString(); 
    } 

    private void tan_Click(object sender, EventArgs e) 
    { 
     second = Math.Tan(second); 
     resultarea.Text = second.ToString(); 
    } 

    private void nfac_Click(object sender, EventArgs e) 
    { 
     double aux = second; 
     if (second == 0) 
      aux = 1; 
     else 
     for (double i = 1; i < second; i++) 
      aux= aux*i; 
     second = aux; 
     resultarea.Text = second.ToString(); 
    } 

    private void log_Click(object sender, EventArgs e) 
    { 
     second = Math.Log(second); 
     resultarea.Text = second.ToString(); 
    } 

    private void ln_Click(object sender, EventArgs e) 
    { 
     second = Math.Log10(second); 
     resultarea.Text = second.ToString(); 
    } 

    private void xexp_Click(object sender, EventArgs e) 
    { 
     second = Math.Pow(second ,2); 
     resultarea.Text = second.ToString(); 
    } 

    private void dot_Click(object sender, EventArgs e) 
    { 

    } 

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.D1 || e.KeyCode == Keys.NumPad1) 
     { 
      viewarea.Text = "fgdgd"; 
      //number(1); 


     } 
     else if (e.KeyCode == Keys.D2 || e.KeyCode == Keys.NumPad2) 
     { 
      number(2); 
     } 
     else if (e.KeyCode == Keys.D3 || e.KeyCode == Keys.NumPad3) 
     { 
      number(3); 
     } 
     else if (e.KeyCode == Keys.D4 || e.KeyCode == Keys.NumPad4) 
     { 
      number(4); 
     } 
     else if (e.KeyCode == Keys.D5 || e.KeyCode == Keys.NumPad5) 
     { 
      number(5); 
     } 
     else if (e.KeyCode == Keys.D6 || e.KeyCode == Keys.NumPad6) 
     { 
      number(6); 
     } 
     else if (e.KeyCode == Keys.D7 || e.KeyCode == Keys.NumPad7) 
     { 
      number(7); 
     } 
     else if (e.KeyCode == Keys.D8 || e.KeyCode == Keys.NumPad8) 
     { 
      number(8); 
     } 
     else if (e.KeyCode == Keys.D9 || e.KeyCode == Keys.NumPad9) 
     { 
      number(9); 
     } 
     else if (e.KeyCode == Keys.Divide) 
     { 
      sign("/"); 
     } 
     else if (e.KeyCode == Keys.Subtract) 
     { 
      sign("-"); 
     } 
     else if (e.KeyCode == Keys.Add) 
     { 
      sign("+"); 
     } 
     else if (e.KeyCode == Keys.Enter) 
     { 
      sign("="); 
     } 
    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 


} 
+0

是不觸發? – Shai 2011-12-27 11:24:23

+1

它是Winforms還是Wpf?你有沒有其他的表格控制,誰可能有重點? – Fischermaen 2011-12-27 11:25:38

+0

@Fischer,我們可以通過KeyEventArgs看到它,它是Windows窗體。 – Adam 2011-12-27 12:02:31

回答

14

假設你有一個WinForms項目,表單的KeyPreview屬性設置爲true,這樣的(例如,在構造函數):

public Form1() 
{ 
    InitializeComponent(); 
    KeyPreview = true; 
} 

,它應該像你的預期。

編輯:由於您的評論我已經添加了代碼來捕獲所有的跡象(添加到您的Form1_KeyDown法)

if (((e.KeyCode == Keys.D7) && (e.Modifiers == Keys.Shift)) || (e.KeyCode == Keys.Divide)) 
{ 
    sign("/"); 
} 
else if (((e.KeyCode == Keys.Oemplus) && (e.Modifiers == Keys.Shift)) || (e.KeyCode == Keys.Multiply)) 
{ 
    sign("*"); 
} 
else if ((e.KeyCode == Keys.OemMinus) || (e.KeyCode == Keys.Subtract)) 
{ 
    sign("-"); 
} 
else if ((e.KeyCode == Keys.Oemplus) || (e.KeyCode == Keys.Add)) 
{ 
    sign("+"); 
} 
else if (e.KeyCode == Keys.Enter) 
{ 
    sign("="); 
} 
+0

伴侶我已經看到了你的答案,我喜歡你自信地添加**的方式,它應該可以工作像你期望的那樣** +1 :-) – 2011-12-27 11:33:23

+0

@PankajUpadhyay:謝謝! :-D – Fischermaen 2011-12-27 11:44:27

+0

仍然沒有,我把我的整個代碼在我的第一篇文章 – 2011-12-27 12:47:20

相關問題