Hello,

Please disregard any small syntax errors I have made in this question, I’m trying to simplify a program that is many pages long.

I have a program that has 2 parts, main.cpp and a form GUI.h. Both have functions that pass variables between themselves and I don't want to combine the two due to other aspects of their functionality. The form is loaded from running the main program using the Application::Run command.

The idea of the form is that it is constantly open (viewable) and updates the values that it shows based on either: a) algorithm run in main.cpp; or b) input from the user (via buttons on the form itself).

The program takes turns to accept decisions from the computer and user. I want to find a way to get the program execution to pause and wait for the user to make their decision (via one of 2 buttons) then continue when the decision is made (one of the buttons is pressed). I have added a simplified version of the code below, which adds or subtracts 1 from a running total:

GUI.h

namespace NGUI 
{
// <namespace declarations>

public ref class GUI : public System::Windows::Forms::Form
{
int running_count; bool computer_turn; bool user_turn; bool still_user_turn;
    public:
    GUI(void)
    {
    InitializeComponent(); // usual form code detailing form construction 
    running_count = 10;
    computer_turn = true;
    user_turn = false;	
			
        for (int y = 0, y < 5, y ++) // repeat procedure a few times
        {
        if (computer_turn == true)
            {
// <execute one of the functions (plus or minus 1 from running total) based on an         main.cpp based algorithm>
            }
        else if (user_turn == true)
            {
                while (still_user_turn == true)
                {
// <wait until button is pressed and action related function functions (plus or minus 1 from running total), then continue program>
                }
            }
        if (computer_turn == true) {computer_turn = false; user_turn = true; }
        if (user_turn == true) {user_turn = false; computer_turn = true;}
// <update form label with new values >
        }	
    }
public:
    int function_plus_1(int x)
    {
    x = x + 1;
    return x;
    }
public:
    int function_minus_1(int x)
    {
    x = x - 1;
    return x;
    }
// <code>
// <Windows Form Designer generated code>
}

// Button click events:
private: System::Void button1_Click(System::Object^sender, System::EventArgs^e) 
    {
    running_count = function_plus_1(running_count);
    still_user_turn = false;
    }
private: System::Void button2_Click(System::Object^sender, System::EventArgs^e) {
    {
    running_count = function_minus_1(running_count);
    still_user_turn = false;
    }

};
}

main.cpp:

// <code>
int _tmain(int argc, _TCHAR* argv[])
{
// <code>
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    Application::Run(gcnew GUI()); // load up form

    return 0;
}

I assume you use some combination of while / break statements to pause and continue the program when the button is pressed but I cannot seem to get it to work. I am currently trying to use a while statement, as shown above, with the still_user_turn boolean variable but the buttons seem inoperable when the form loads.

Any help would be greatly appreciated.

Edited 6 Years Ago by carrythe1: n/a

#include <stdio.h>
void main(void)
{
char option;
printf("Press A for Alpha");
printf("press B for Beta");
option = getch();

switch(option)
{
case 'A':
Alpha();
break;

case 'B':
Beta();
break;
}
}

Heres a super simplified version of a stop and wait for a decision making. hope it helps

thanks - i'm familiar with SWITCH but isn't this just a way of simplifying many IF statements? I still don't know how i would, for example, get the program to use the following logic:

<run code and get to input decision point>
<pause main program and wait for input>
if (<button 1 on form is pressed>) {<then carry out function_1>}
else if (<button 2 on form is pressed>) {<then carry out function_2>}
<continue code>

as i believe you need to feed a value into the switch statement in order for it to decide what to do?

Maybe this is more of a code syntax issue than a logical issue, it seems i am looking for the equivalent of the 'option = getch()' statement in you're example - but instead of waiting for a button-press input from the keyboard, i need the input to come from the user as a mouse-click on a button on the form.

Edited 6 Years Ago by carrythe1: n/a

its a form application in c++ but i need native functions as well as managed functions

thanks - i'm familiar with SWITCH but isn't this just a way of simplifying many IF statements? I still don't know how i would, for example, get the program to use the following logic:

<run code and get to input decision point>
<pause main program and wait for input>
if (<button 1 on form is pressed>) {<then carry out function_1>}
else if (<button 2 on form is pressed>) {<then carry out function_2>}
<continue code>

as i believe you need to feed a value into the switch statement in order for it to decide what to do?

Maybe this is more of a code syntax issue than a logical issue, it seems i am looking for the equivalent of the 'option = getch()' statement in you're example - but instead of waiting for a button-press input from the keyboard, i need the input to come from the user as a mouse-click on a button on the form.

I'd say it's both a syntax and a design issue. I've never been a .NET GUI programmer, so not much help there, but I believe (fingers crossed, don't take this to the bank) that in many ways it's similar to Java Swing GUI programming in that the buttons have "Listeners" and when you press the button, whatever code was being run before will pause, the Listener function will be executed, then when it's done, control is returned to whatever was going on before the button was pressed (frankly, my Java GUI is kind of rusty too, so don't quote me on THAT either :)).

So an inefficient, but probably workable and fairly simple solution would be to have the Button's Listener function (apparently button1_Click here) set some boolean flag that is checked in the code that is waiting for a user action:

bool button1Pressed = false; / global variable.

// more code

while (still_user_turn == true)
{
// <wait until button is pressed and action related function functions (plus or minus 1 from running total), then continue program>
   while (!button1Pressed)
   {
      // empty loop?  Maybe stick a pause in there?
   }

   // button 1 has been pressed.  Do whatever.
}

Within that button1_Clicked function, set button1Pressed to true. This is a form of "busy waiting" and is generally frowned upon. There are better solutions out there that involve some redesign, but I imagine something like this might do the trick for you.

Again, I'm not a .NET programmer, but the general process is to to set some type of "you can proceed" flag, check it constantly ("busy waiting", again frowned upon often) and the function that handles the button click sets that flag. Somewhere you may also need to reset it to false.

i kind of tried this and realized that the buttons didn't become active until the entire 'Application::Run(gcnew GUI());' command had been run, which was why the button presses were not working.

I've since redsigned to allow the GUI function (containing the InitializeComponent() function) to complete which then allows the buttons to become active. The program then moves around the class using functions within the functions connected to button presses. Unfortunately i'm now getting lots of stack overflows due to recursivity and the logic is tedious at best.

I think its just a tough design issue,

thanks for the help!

There is a specific property for Forms. It is Enabled. I'm not sure about MFC as I do only VCL.
If however they are separate thread, which I doubt they are, I recommend using WaitForInputIdle(); GDI function.

This question has already been answered. Start a new discussion instead.