在timer.stop()之后禁用表单的KeyDown

时间:2014-07-09 09:03:10

标签: c# timer windows-forms-designer keydown

我刚刚创建了一个字母游戏。当计时器打开时,它会将字母打印到列表框中。 用户必须按正确的键。如果他这样做,则更新具有正确按压数量的标签。如果没有,则更新遗漏的标签。在这两种情况下,Total和精度标签都会更新。 问题是游戏结束后仍然很重要。 我想要做的是禁止在窗体上发生keyDown事件。 我写了这段代码但是没有用。

timer1.Stop();
Form1 form = new Form1();
form.KeyPreview = false;

有没有人有解决方案?

这是我的代码。即使我添加了一个标志,表单即使在游戏结束后也会启用keyDown事件。我想知道游戏结束后是否有办法禁止发生keyDown事件

命名空间TypingGame {     公共部分类Form1:表格     {         随机随机= new Random();         统计数据=新统计数据();

    public Form1()
    {
        InitializeComponent();
    }

    //Here the game starts
    private void timer1_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Add((Keys) random.Next(65,90));
        //If letters missed are more than 7 the game is over
        //so I need a way to disable the KeyDown event from happening after the game is over
        if (listBox1.Items.Count > 7)
        {
            listBox1.Items.Clear();
            listBox1.Items.Add("Game Over");
            timer1.Stop();
        }
    }
    //Here is what happens on KeyDown.
    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 + "%";

    }
}

}

1 个答案:

答案 0 :(得分:1)

我已根据您的代码更新了代码库。

MSDN Reference - Timer

//Here the game starts (?? game stop?)
private void timer1_Tick(object sender, EventArgs e)
{
    listBox1.Items.Add((Keys) random.Next(65,90));
    //If letters missed are more than 7 the game is over
    //so I need a way to disable the KeyDown event from happening after the game is over
    if (listBox1.Items.Count > 7)
    {
        listBox1.Items.Clear();
        listBox1.Items.Add("Game Over");
        // timer1.Stop(); 
        // disable the timer to stop
        timer1.Enabled = false;
    }
}
//Here is what happens on KeyDown.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    // if timer disabled, do nothing
    if (!timer1.Enabled) return;

    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 + "%";

}