Member Avatar for arcticM

Hey everybody!
in the book "Head First C#" there's a code for a program that is a fast typing game. for those of u who don't know it here are few details:
there's a listBox and using a timer, random letters are added to the listBox until there are 7 letters in the list and the game is over. so let's say the list has "S J K" and u press "s" on the keyboard, the S is removed from the list and the temp of adding new letters speeds up until u can't keep up and it's over.

NOW!
I'm trying to change the code so that instead of adding random letters it will add letters in a specific order of a specific word(let's say "home"), so the letters would be like "H O M E H O M E H O M E..."
the original code:

private void timer1_Tick(object sender, EventArgs e)
        {
            // Add a random key to the ListBox
            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 the user pressed a key that's in the ListBox, remove it
            // and then make the game a little faster
            if (listBox1.Items.Contains(e.KeyCode))
            {
                listBox1.Items.Remove(e.KeyCode);
                listBox1.Refresh();
                ...
                ...
            }

ok, so what I tried to do was to make a string[] arr= {"h","o","m","e"}; and instead of

listBox1.Items.Add((Keys)random.Next(65, 90));

do this

listBox1.Items.Add(arr[i]);

I did the i++ and all that.. so the result was that the letters did show up correctly in the textbox BUT when I was pressing the keyboard nothing happened!! :(
it might be because I left out the (keys) from listBox1.Items.Add((Keys)random.Next(65, 90));
but when I tried to add it like this ..(keys)arr.. it says can't convert from string to keys..

any ideas on how to make this thing work?:?:

Recommended Answers

All 2 Replies

Try to figure out this code bellow, I dont know exactly what is the game about, so I midified it a bit, only to see how to use a string array in the code:
I didnt use the timer, but I pick up the letters on a button click event.

public partial class Form2 : Form
    {
        Random random = new Random();
        string[] array = new string[] { "H", "O", "M", "E" };
        int i = 0;
        public Form2()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count < 4)
            {
                if (i == 0)
                    listBox1.Items.Clear();
                listBox1.Items.Add(array[i]);
                i++;
            }
            else
            {
                listBox1.Items.Clear();
                listBox1.Items.Add("Game over");
                i = 0;
            }
        }

Let me know if this is not it (and can you please give me whole code you got there, that I can imagine whats all about.
thx

Mitja

Member Avatar for arcticM

I didn't want to publish the entire code because of the book's copy rights but I see everyone can download the whole solution here legaly :)
http://headfirstlabs.com/books/hfcsharp/
go to chapter 4 -> and download the "Hit the Keys"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.