C# Make Program Wait For Button To Be Pressed

后端 未结 3 692
有刺的猬
有刺的猬 2021-01-18 08:04

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

相关标签:
3条回答
  • 2021-01-18 08:15

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-18 08:26

    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.

    0 讨论(0)
  • 2021-01-18 08:34

    If you need the program to wait for that event then don't write the waitForTheButton code in your main access/Page load.

    Instead write your code inside the event. So you don't have to make an active wait. Events are for that.

    eg: If you have something like this

    static void Main()
    {
        while (notPressed) { }
        DoThis();
        DoThat();
    }
    

    Then you could be changing it to:

    static void Main
    {
        myButton.OnClick += myButton_EventHandler;
    }
    
    private void myButton_EventHandler(object sender, EventArgs e)
    {
        DoThis();
        DoThat();
    }                                     
    

    If you really want to work in a procedural paradigm even when you are using a OOP language, you should use a Semaphore or something like that. But that doesn't seem to be what you actually need, It's more like something you want.

    0 讨论(0)
提交回复
热议问题