Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
I am writing a program that has the alphabet on the screen and randomly says a letter. Then the user is to click on a letter (whether right or wrong) and then move on to the next for statement until all ten are done. How do I get the program to pause until the user selects a letter on the screen before moving on? The letters are buttons on the screen that are clicked on with a mouse by the kids. Here is what I have so far:
private void StrtGmBtn_Click(object sender, EventArgs e)
    //Set variables
    string correct = "Your right!  Great Job!!";
    string incorrect = "Sorry thats wrong:(";
    int count = 10; //Number of times to play in one game
    char[] pick = new char[count];//Variable for each selection
    for (int i = 0; i < count;)
        pick[i] = GetRndm();
        if (LetterChoice == pick[i])
            AnswersTxt.Text = correct;
            AnswersTxt.Text = incorrect;

public static char GetRndm()
    // This method returns a random lowercase letter.
    // ... Between 'a' and 'z' inclusize.
    int num = _random.Next(0, 26); // Zero to 25
    char let = (char)('a' + num);

    return let;
private void  SayLetter(char a)
    //char let = (char)('a' + a);
     SoundPlayer Game = new SoundPlayer
                   (@"" + a + ".wav");
Thanks in advance for any help! I am new and just trying to write something for my kids to play with!
Updated 25-Feb-12 11:15am
Sander Rossel 25-Feb-12 15:09pm    
Posting such a long piece of code isn't going to get you friends here. It costs us quite some time to read it all, and most of it is irrelevant. Posting it AGAIN is a sin punished by probably not getting an answer. Next time if you want to update your question use the 'Improve question' button on the lower right side of your question instead of posting your fix as an answer to your own question. I suggest you use the 'Improve question' button now to remove all the irrelevant code so we can help you better.
Sergey Alexandrovich Kryukov 25-Feb-12 15:34pm    
Are you serious? Hoping for improving the question? Did you read a title of it?
Anyway, no improvement is needed. Poor kids -- the father tries to teach them typing, while his own problem is typing too much, using hands instead of using his brain.
I answered, please see.
Sander Rossel 25-Feb-12 15:42pm    
Haven't really read the code, looked to fishy for my taste... Also, I was wondering how you could NOT have a WinForm app waiting for user input, but then again, I haven't read the code either.
James M. White 25-Feb-12 15:46pm    
I created a CASE statement that has the random letters assigned and want to wait until the user clicks on one of 26 buttons on the form to then move on to the next case. Should I be doing this in another way?
LanFanNinja 25-Feb-12 18:05pm    
Check out my solution. I have posted some code for you to study.

Hi! I have written some code for you to check out. You should be able to create a new windows forms project and just past this code in place of the class Form1 code that is generated.

This is a complete solution I wrote for you to give you some ideas of how you could do something like this using a winforms project.

NOTE: I would suggest you download all the sound files from the url you are passing to the sound player so you can be sure they are available when you go to play them.

If you have any questions about the code just leave me a comment and I will try to help you out.

public partial class Form1 : Form
    Button[] alphaButtons = new Button[26];
    char[] charsToSay = new char[10];
    char letterChoice;
    int charIdx = 0;

    public Form1()

        int asciiCode = 65;
        for (int i = 0; i < alphaButtons.Length; i++)
            alphaButtons[i] = new Button();
            //add the button control to the form
            alphaButtons[i].Size = new System.Drawing.Size(20, 32);
            int prevIdx = i == 0 ? 0 : i - 1;
            alphaButtons[i].Location = new Point(
                alphaButtons[prevIdx].Location.X + alphaButtons[0].Width, 10);
            alphaButtons[i].Text = Convert.ToChar(asciiCode).ToString();
            alphaButtons[i].Click += new EventHandler(AlphaButtons_Click);


        letterChoice = charsToSay[charIdx];


    private void Play()

    private void GetRand()
        // change randomness as desired

        Random rand = new Random();

        for (int i = 0; i < charsToSay.Length; i++)
            charsToSay[i] = Convert.ToChar(rand.Next(65, 90));//ascii chars A - Z

    void AlphaButtons_Click(object sender, EventArgs e)
        Button b = sender as Button;
        char c = Convert.ToChar(b.Text);

        if (c == letterChoice)
            MessageBox.Show("Congratulations that is Correct!!!");//test
            MessageBox.Show("Sorry that is incorrect!");//test

        //you may want to wait a second or two before saying next char
        // i.e. Thread.Sleep(2000);
        //just depends on what you want

        if (charIdx < charsToSay.Length)
            letterChoice = charsToSay[charIdx];
            // GAME OVER
            MessageBox.Show("GAME OVER!");//test

    private void SayLetter()
        string c = letterChoice.ToString().ToLower();
        SoundPlayer sound = new SoundPlayer(
            @"" + c + ".wav");
Share this answer
James M. White 25-Feb-12 19:20pm    
Thank you so much that is awesome!!!!!! Now I just have to spread the buttons over three rows and keep a score!! I will get that! Again I can't thank you enough!!!
LanFanNinja 25-Feb-12 19:53pm    
You're welcome. Glad that it helped.
Sander Rossel 26-Feb-12 3:10am    
My 5 for the answer. Nice trick with the buttons! I feel stupid for not thinking of that myself :laugh:
LanFanNinja 26-Feb-12 4:13am    
Thank you! :)
Hmmm... Now I am sure there's some people here that wouldn't agree with my solution, but here is what you could do (forget everything you got so far):
Create an array containing every letter of the alphabet. Then create a List<TextBox>[^] containing all your TextBoxes in alphabetical order (so TextBoxA first, TextBoxB second, TextBoxC third etc...).
Hook up all TextBox.Click Events[^] to the same listener.
When a TextBox is clicked cast the sender to a TextBox and find its position in the list using IndexOf[^]. Now you got the index you can use this to get the corresponding letter in the array.
To get a random letter you can still use Random.Next(0, 26)[^] and fetch the letter at that specific index from the array. See if you can do anything with this. It shouldn't be to difficult to write and apart from creating the list with all your TextBoxes I think it will greatly reduce your lines of code :)
I am not sure what you needed the huge switch statement for in your original code.
Good luck!
Share this answer
James M. White 25-Feb-12 16:19pm    
Thank you Naerling! My problem is that I have 26 buttons representing the alphabet for kids 3 and under to use the mouse to click on when prompted by the audio for a letter. This is not a keyboard event, so the only input I need to wait for is when they click on a button. Can I write a for statement that generates the audio with a pause for user input (buttons on the screen) using interop?
Sander Rossel 25-Feb-12 16:36pm    
Your Button.Click Event will fire automatically when a button is activated in any way. Until then your application will simply stay and do nothing. No need to do anything there...
Simply handle Button.Click Events. To block your UI thread while the sound is playing simply use SoundPlayer.PlaySync (you might want to put your buttons in a Panel and set Panel.Enabled = False before you call PlaySync and Enable = True after).
There is no such thing as pause in UI applications. More exactly, the main threads of such application permanently sit in a wait mode wasting zero CPU time. The OS wakes up a thread only when the user tries to input something with a keyboard and a mouse.

In this way, the question does not have any sense, even remotely. There is nothing to discuss at all.

I even don't know what to advice. Unfortunately, I am not sure if helping you can be possible at all, honestly.

Here is why. For developing software, even the simplest one, you need to be a software developer, a programmer. After you have written the "code" like
private void ZBtn_Click(object sender, EventArgs e)
case 6:
26 times each(!), you demonstrated that you are doing something directly opposite to programming. A programmer would never try to repeat any code twice ([^]). You are writing something totally useless from the very beginning to the very end. Even the simple laziness did not save you from typing all that and even is a question. How can I hope that any advice can help?

Share this answer
James M. White 25-Feb-12 15:44pm    

How then should I write the buttons? Make an array that creates them? I have images that go on each button as well. Like I said I am new and will take any and all advice.

Thanks, Jim
Sander Rossel 25-Feb-12 16:17pm    
Though I think SA's answer is somewhat justified (your code really isn't very nice) I believe you have good intentions and will actually try to make any suggestion we make work. So I have tried to explain a pretty easy and straightforward method with which you can make this work. See my answer, try out some stuff, and if you get really stuck you can come back for another question. Good luck! :)
Sergey Alexandrovich Kryukov 25-Feb-12 22:13pm    
Is the code by LanFanNinja enough?

You are asking about some buttons, what are they for? Why some rows? What's the score.
For score, I would suggest this: a score should be a time needed to type N characters, where N is setup in a UI.
The time should be measures using the class System.Diagnostics.Stopwatch.

You don't need buttons except just one: Start. After start, disable it. When N characters are typed, Start appears as enabled and resulting time spent on typing is shown. The character to be shown can be show as a label.

It's also good to type in some TextBox. The student should be required to remove mistyped character, so on each step the whole text should be equal to the one auto-generated, otherwise next character to type is not shown. You can generate a required characters in advance, in one string. You can compare it in a case-sensitive or case-insensitive manner.

Sander Rossel 25-Feb-12 15:49pm    
That's a bit harsh... Here's a loving father trying to make something nice for his kids and you call it useless (there are still programmers with an 'if it works it works'-attitude).
The OP may not be a (good) programmer, but at least he's trying (we can't all be super programmers).
Sergey Alexandrovich Kryukov 25-Feb-12 22:04pm    
I know, I know. You see, it makes no difference. I sympathize with him, too, but how can I help? I even had the idea was to spend half an hour and write a working application for him. This code is not just good or bad, it's helpless. I could try to teach OP if we had a chance to sit together, but not in quick answer. Any small fix would make it only worse. By the way, did you see that I actually answered the question first?

Maybe "don't waste your time" would be more useful then explaining how to write simple programs? It it really too hard to do in one quick answer.

By the way, I also dislike the idea of the application. There are some typing training application, I believe some of them would be more reasonable.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900