Hey.
I'm pretty new to C# and programming overall.
I'm trying to make a Hangman game. Let's say the user want to make a guess at the letter "t". Then he should just have to press "t" on his keyboard and the form should recognize it. I currently can't even get the application to react to keys that are being pressed down.

Once I get this going I should be able to do the rest on my own so I really need help with this. Thanks!

First you would need gain input from the user then the input would need to be validated using something like an if else or switch statement. For instance:

ps i have not programmed in c# in a while.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream.h>
char  Hang ;


int main()
{
// General Stuff about your program
}
scanf () // Ask for Input


if( Hang=="t")
{
//do something
}
else if(Hang=="c")
{
// do something
}
else
{
//do something
}

or you can use a switch statement like so:

switch (myInt)
{
case '1':
//do something
break;
case '2':
//do something
break;
case '3':
//do something
break;
default:
//error, incorrect input
break;

Simply adjust the statement or statements to facilitate your needs.
hope this has helped, feel free to drop me a line if you require further help. :)

I think you need ReadKey function, which reads one character, pressed by the user on his keyboard. Or you can use Read() function, which will return digital code of the symbol (type int) The statement is:

Console.ReadKey();   //returns ConsoleKey type, which contains all keyboard keys


char a;
a = Console.Read();
if (a == 't')
{
//do something
}

Thanks for the replies. Volgent I'm using Form and I think that console and form isn't the same, or is it?

As for Ice Mans reply, what are those "#include <stdio.h>" and where do I put them?

I've gotten the Form to react to keystrokes but it doesn't identify them as it is reacting the same whether key you press.
I've done that by simply doing an KeyDown event on the Form but I don't know how to identify which key is pressed down. :(

Um ignore IceMans post as the example is in C/C++.

To do this using a form is quite simple. In the properties of designer select the events tab and double click the gap next to KeyPress to create the event handler code for you. Now do somthing like below:

private void Dialog_KeyPress(object sender, KeyPressEventArgs e)
{
            switch (e.KeyChar)
            {
                case 'a':
                case 'b':
                case 'c':
                default: break;
            }
}

Ah thanks privatevoid! But.. How do I use them in an 'if' validation? Argh i feel so worthless at this :(

If I understand right, you'll need some kind of that:

private void Dialog_KeyPress(object sender, KeyPressEventArgs e)
{
       if (e.KeyChar == 'a')
      {
      }
}

If I understand right, you'll need some kind of that:

private void Dialog_KeyPress(object sender, KeyPressEventArgs e)
{
       if (e.KeyChar == 'a')
      {
      }
}

I'm afraid that didn't do the trick :S

It depends how you want your program to work, if you have a word in a string somwhere to compare against you could do somthing like this:

private string word = "fodder"; 

private void Dialog_KeyPress(object sender, KeyPressEventArgs e)
{       
     for(int index=0; index < word.Length; index++)
     {
           if( word[index] == e.KeyChar)
           {
                 // Found a letter e.KeyChar at index of work 
                 // (now do somthing with it)
            }
     }
}

It depends how you want your program to work, if you have a word in a string somwhere to compare against you could do somthing like this:

private string word = "fodder"; 

private void Dialog_KeyPress(object sender, KeyPressEventArgs e)
{       
     for(int index=0; index < word.Length; index++)
     {
           if( word[index] == e.KeyChar)
           {
                 // Found a letter e.KeyChar at index of work 
                 // (now do somthing with it)
            }
     }
}

Yes! That was exactly what I needed. I think I should be able to do this now. Thank you so much!
Let's just hope I don't run into any more obstacles now ;)

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.