C# Make Program Wait For Button To Be Pressed

37,838

Solution 1

Use events - it's what they're designed for.

You don't need to use a boolean variable for this in the Button_Click event handler call your code:

private void Button_Click(object sender, EventArgs e)
{
    // The code you need to execute when the button is pressed
}

As @trickdev points out you will need to subscribe to this event but if you use the Events window in Visual Studio it will add the necessary code - including the empty handler - for you.

With event driven programs you are always waiting until the next "thing" happens. So in your case (if I've understood your application correctly) when you start the program it should simply tell the first button to flash "N" times. If you write that as event then the application will return to the waiting state once the code has completed.

Then in the button click event handler - you can subscribe all the buttons to the same event - you can check that the correct button has been pressed and then tell the next button to flash. If the wrong button was pressed then display a suitable message.

So in pseudo code you have:

public class Form
{
    Initialise()
    {
        this.Loaded += FormLoaded;
    }

    private void FormLoaded(object sender, EventArgs e)
    {
        // pick a button
        pickedButton.Flash();
    }

    private void Button_Click(object sender, EventArgs e)
    {
        if (sender == pickedButton)
        {
            pickedButton = pickButton();
        }
        else
        {
            message = "Sorry wrong button, try again";
        }

        pickedButton.Flash();
    }
}

public class Button
{
    public void Flash()
    {
        // loop N times turning button on/off
    }
}

Solution 2

Windows Forms Controls have a Click event you can subscribe to in the form constructor:

myButton.Click += myButton_EventHandler;

You can then put whatever logic you want to happen in the handler and this will execute when the button has been clicked:

private void myButton_EventHandler(object sender, EventArgs e)
{
    Redpress = false;
}

You should avoid blocking (in any way, spinning sleeping etc) the Main thread in your forms applications as this will lock up the interface, there are many methods to avoid this including Timers, Threads, Delegates and BackgroundWorkers to name a few.

EDIT: To include your update

For this you could use a ManualResetEvent.

private readonly ManualResetEvent mre = new ManualResetEvent(false);

private void myButton_EventHandler(object sender, EventArgs e)
{
    mre.Set();
}

Your Form code can wait by calling:

mre.WaitOne();

This will make the executing code wait until the event has fired. Hope that helps.

NOTE: Please don't be mistaken though, unless you have some special case (I can't think of one off the top of my head at this time of night!) you should put the code straight in the event handler, rather than blocking a thread until the event has fired.

Share:
37,838
aelsheikh
Author by

aelsheikh

Updated on July 09, 2022

Comments

  • aelsheikh
    aelsheikh almost 2 years

    I want to make the program wait for a button to be pressed before it continues, I tried creating a while loop and having it loop until the button is clicked and sets a bool to true for the while loop to end, this made it crash

    while (!Redpress)
    {
        // I'd like the wait here
    }
    
    Redpress = false;
    

    It doesn't matter whether or not the while is there, as long as the program waits for the button press before setting "Redpress" to false... Any Ideas?

  • aelsheikh
    aelsheikh about 13 years
    That doesn't make sense, redpress is a bool that defines whether the button has been clicked or not, I already have an event to set the bool to true when the button is pressed. I need code to make the program wait for the event...
  • ChrisF
    ChrisF about 13 years
    @Zizo47 - sorry I misunderstood - put the code you need in the handler where I've indicated with the comment.
  • aelsheikh
    aelsheikh about 13 years
    All you done there is set redpress to false on with an event, I need the program to wait for "that" event...
  • aelsheikh
    aelsheikh about 13 years
    This still doesn't change a thing, I need to the program to stop and wait for me to click the button, I have no problems with the button itself.
  • ChrisF
    ChrisF about 13 years
    @Zizo47 - what do you mean by "stop". If you put the code inside the event handler it won't get executed until the button is pressed.
  • aelsheikh
    aelsheikh about 13 years
    Let me explain a bit more, this is a simon says game, its waiting flashes the button the user has to press, then "waits" for the user to press it. I want to make it wait within the constraints of that code, so It wont continue so long as the button is pressed.
  • ChrisF
    ChrisF about 13 years
    @Zizo47 - I don't fully understand the problem - you want the button to flash "N" times and then stop, and the next button only flash when the user clicks on the first one? In that case all you "main" code needs to do is tell the first button to flash. The program will then wait until it gets further input - in this case the button press. If it's the right one you can then tell the next button to flash.
  • aelsheikh
    aelsheikh about 13 years
    Don't worry, I've done a bit of research and apparently what I'm asking for is pretty damn hard. I'll just change the whole code around, thanks for your time anyway.