2013-05-21 53 views
1
private void Form1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (listBox1.Items.Contains(e.KeyCode)) 
    { 
     listBox1.Items.Remove(e.KeyCode); 
     listBox1.Refresh(); 
     timer1.Interval -= 10; 
     difficultyProgessbar.Value = 800 - timer1.Interval; 
     stats.update(true); 
    } 
    else 
    { 
     stats.update(false); 
    } 

    correctLabel.Text = stats.correct.ToString(); 
    missedLabel.Text = stats.missed.ToString(); 
    totalLabel.Text = stats.total.ToString(); 
    accuracyLabel.Text = stats.accuracy.ToString(); 

} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    //Add a random key to Listbox 
    listBox1.Items.Add((Keys)random.Next(65, 90)); 
    Application.DoEvents(); 
    if (listBox1.Items.Count > 7) 
    { 
     listBox1.Items.Clear(); 
     listBox1.Items.Add("Game Over"); 
     timer1.Stop(); 
    } 
} 

當我運行我的應用程序,timer1_Tick事件工作正常,但是當我按任意鍵Form1_KeyDown事件不會執行。 有什麼缺失?爲什麼Key_Down事件不會觸發? 謝謝Form1_KeyDown不工作

+1

您的表單KeyPreview屬性是否設置爲true? –

+0

http://stackoverflow.com/a/1298733/2179864 – Zigma

回答

5

Keydown在與焦點控制火災。
要在表單級別接收它,您需要設置屬性。 KeyPreview =表格爲真

+0

Thanks.Problem解決。 –