Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to exit a while loop with a button press?

**** PREVIOUSLY POSTED IN THE CC++ MESSAGE BOARD *****

This seems like a simple thing to accomplish but I cannot get it figured out.

I'm running a while loop that is started by a button's OnClick event. I want to use another button's OnClick event to break out of the while loop started by the first button.

For example:

Form1::Button1OnClick(){
while(bContinueLooping){
//Code goes here
}
}

Form1::Button2OnClick(){
bContinueLooping = false;
}

Desired results:
Clicking Button1 causes the loop to start.
Clicking Button2 causes the loop to end.

The problem is that as long as the while loop is running, Button2 doesn't get recognized.

Anyone know how to break out of a while loop using this "press a button to exit" method or have a suggestion for a better way to handle it?

This is a 32-bit Windows program.

Comments

  • patrick starpatrick star Member Posts: 3
    : **** PREVIOUSLY POSTED IN THE CC++ MESSAGE BOARD *****
    :
    : This seems like a simple thing to accomplish but I cannot get it figured out.
    :
    : I'm running a while loop that is started by a button's OnClick event. I want to use another button's OnClick event to break out of the while loop started by the first button.
    :
    : For example:
    :
    : Form1::Button1OnClick(){
    : while(bContinueLooping){
    : //Code goes here
    : }
    : }
    :
    : Form1::Button2OnClick(){
    : bContinueLooping = false;
    : }
    :
    : Desired results:
    : Clicking Button1 causes the loop to start.
    : Clicking Button2 causes the loop to end.
    :
    : The problem is that as long as the while loop is running, Button2 doesn't get recognized.
    :
    : Anyone know how to break out of a while loop using this "press a button to exit" method or have a suggestion for a better way to handle it?
    :
    : This is a 32-bit Windows program.
    :
    :

    I don't know if this will make it right or not, but it's my suggestion.
    Put a call procedure inside Form1.
    That procedure consist of some code that check whether Button2 is clicked or not. But it may waste the time of the execution.

    Form1::Button1OnClick(){
    while(bContinueLooping){
    if(Button2 is clicked){
    bContinueLooping = false;
    }
    //Code goes here
    }
    }

    I don't know what compiler you use. But i think it must have a call procedure to detect whether a button is clicked or not.
    I hope this helps.
  • paulhaynespaulhaynes Member Posts: 5
    : **** PREVIOUSLY POSTED IN THE CC++ MESSAGE BOARD *****
    ***Answer copied from CC++ message board ***

    : : : Form1::Button1OnClick(){
    : : : while(bContinueLooping){
    : : : //Code goes here
    Application->ProcessMessages();
    : : : }
    : : : }

    :
    : What you want to add is the following line:
    :
    :
    : Application->ProcessMessages();
    :

    :
    : Now all new OnClick Events will be processed.
    :
    : See ya,
    : bilderbikkel
    :
    :
    Added the suggested line as indicated in the sample code above and it works like a charm. Thanks much. Onward to the next hurdle :)

Sign In or Register to comment.