0

Hi everyone,
I'm trying to write this code.
Whenever a user types a letter that matches what is already displayed, it should clean up the list box..the problem here is it is not detecting the keyDown event. What is wrong?
Thanks for the help.

namespace TypingGame
{
    public partial class Form1 : Form
    {

        Status status = new Status();
        Random random = new Random();

        public Form1()
        {
            InitializeComponent();
                    this.KeyDown +=new KeyEventHandler(Form1_KeyDown);
            }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            listBox1.Items.Add((Keys) this.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))
            {
                status.update(true);
                listBox1.Items.Remove(e.KeyCode);
                listBox1.Refresh();
            }
            else
                status.update(false);

            this.CorrectLabel.Text = "Correct: " + status.Correct;
            this.missedLabel.Text = "Missed: " + status.Missed;
            this.accuracyLabel.Text = "Accuracy: " + status.Accuracy;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
    }
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Momerath
0

You've attached the keydown event to the form, is that what you wanted to do? If you are in the listbox when you press the key, it will handle the keypress and not pass it to the form.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.