I have a button in my program that supposed to be clicked after a while loop finished, whats the code to click the button?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To programatically click a button just call the
Clickmethod:Note that this doesn’t cause the UI to update as if the button had been pressed – it just results in the event handler for the click event being run.
In your question you mention that you are running a while loop that presumably takes some time. If you do this in the naive way – running it in the main application thread – it will cause the UI to block while the loop is running. To fix this you need to run the while loop in another thread, for example by using a
BackgroundWorker. But then when your loop finishes you have to be careful to ensure that the click event is called back on the main thread. The general way to do this is to use Invoke, but in the specific case that you have a BackgroundWorker you can run the code after the loop finishes in theOnRunWorkerCompletedevent handler then you don’t need to call Invoke yourself as the BackgroundWorker takes care of this for you.