C# 计时器滴答声事件,赢得';不能被其他功能中断吗?

C# 计时器滴答声事件,赢得';不能被其他功能中断吗?,c#,events,timer,C#,Events,Timer,这是我一直在学习的一本书中的一个游戏 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");

这是我一直在学习的一本书中的一个游戏

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 + "%";
}
它生成随机字母,如果您按下正确的字母,它必须从列表框中删除按下的字母

问题是表单的keydown属性不起作用,它只会突出显示按下的键,但不会将其从列表框中删除,循环会一直持续到列表框已满,然后会给出“游戏结束”消息


我想知道这次我做错了什么,因为整个代码都是从书中提取的?

我已经检查了我的代码和书,问题是它们的Forms KeyPreview设置为True,而我的没有,很遗憾,这就是他们在书中忘记提到的答案。

您是否将该方法添加为表单上相应事件的事件处理程序?另外,我认为应该改为previewKeyDown,假设表单以外的其他内容具有焦点。是的,我添加了该方法,只是尝试了previewKeyDown,它也不起作用。您是否尝试过单独的疑难解答?我的意思是使用
Form1\u键下事件,只需使用
MessageBox.Show(“此处”)或类似的东西来确保事件被触发?我只是尝试了一下,什么都没有发生。有趣的是,我从他们的网站下载了代码,它完全是相同的代码,但我的代码不起作用,而我下载的代码起作用了,我又一次不知所措。