imdrunkisuppose
imdrunkisuppose

Reputation: 131

timer tick event, won't be interrupted by other functions?

So here is a game from a book i've been studying.

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

It generates random letters, and if you press the right letter, it has to remove the letter pressed, from the listbox.

Problem is that Form's keydown property won't work, it only highlights the key pressed, but it won't remove it from the listbox, the cycle continues untill the listbox is full, and it gives game over message then...

I wonder what did i do wrong this time, since the whole code is taken from the book itself?

Upvotes: 0

Views: 325

Answers (1)

imdrunkisuppose
imdrunkisuppose

Reputation: 131

I've checked both my code and the books', the problem was their Forms KeyPreview was set to True, and mine wasn't, so that was the answer which they forgot to mention on the book unfortunately.

Upvotes: 1

Related Questions