Hallo ich versuche das KeyDown Event einer Form zu behandeln.
(Ich versuche das letzte Beispiel aus "Heard First C#" aus Kapitel 4 nachzukochen)
Dazu habe ich auf der Form den EventListener KeyDown angelegt.
Wenn ich das Programm laufen lasse reagiert es nicht auf Tastatureingaben.
Ich habe einen Breakpoint in dem EventListener gesetzt und sehe dass die Methode nie aufgerufen wird.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
| namespace TypingGame { public partial class Form1 : Form { Random random = new Random(); Stats stats = new Stats();
public Form1() { InitializeComponent(); }
private void timer1_Tick(object sender, EventArgs e) { listBox1.Items.Add((Keys)random.Next(65, 90)); if (listBox1.Items.Count > 7) { listBox1.Items.Clear(); listBox1.Items.Add("Game Over"); timer1.Stop(); } }
private void Form1_KeyDown(object sender, KeyEventArgs e) { if (listBox1.Items.Contains(e.KeyCode)) { listBox1.Items.Remove(e.KeyCode); listBox1.Refresh(); if (timer1.Interval > 400) { timer1.Interval -= 10; } if (timer1.Interval > 250) { timer1.Interval -= 7; } if (timer1.Interval > 100) { timer1.Interval -= 2; } difficultyProgressBar.Value = 800 - timer1.Interval; stats.Update(true); } else { stats.Update(false); } correctLabel.Text = "Correct: " + stats.Correct; missedLabel.Text = "Missed: " + stats.Missed; totalLabel.Text = "Total: " + stats.Total; accuracyLabel.Text = "Accuracy: " + stats.Accuracy + " %"; } } } |
Wenn ich runtergeladene Lösung aus dem Buch laufen lasse funktioniert das Programm, aber ich kann keine Unterschiede im Quellcode finden.
Hat jemand eine Ahnung an was das liegen könnte? Muss ich irgendwelche Properties modifizieren damit auf Tastatureingaben reagiert wird?
Vielen Dank für jeden Hinweis,
Hans