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?:?: