Board index » cppbuilder » Break a loop

Break a loop


2004-01-13 04:26:11 PM
cppbuilder95
Hi,
Does anybody know how to make most of
Application->ProcessMessages in combination
with Application->MessageBox to break the loop
and leave TForm1::Button1Click as in my example.
Here is a very simple sample written in C++ Builder v6
for Windows consisting of components: TForm, Edit1, Button1, Button2, Button3.
You can start program by clicking Button1 and it will increase Edit1.
You can press Button2 and answer YES to continue.
Everything seems to be all right until you click Button2 and answer NO
then the program is out of order. Probably the event Button1 is still on.
What to do to release Button1?
Thank in advance.
Mac
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int halt, i;
void TForm1::Increase()
{
while(halt==0)
{
i++;
Edit1->Text=i;
Edit1->Update();
Application->ProcessMessages();
if(halt==1) break;
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
halt=0;
i=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //START BUTTON
{
Increase();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) //BREAK BUTTON
{
if( Application->MessageBox("Do you wish to continue?", "Alert!", MB_YESNO)==7)
{
halt=1;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender) //EXIT BUTTON
{
Application->Terminate();
}
//---------------------------------------------------------------------------
 
 

Re:Break a loop

Hi Mac,
The problem is the way you have your Button1 click handler structured.
I have rewritten the handler and it now works properly.
I have also declared the variables 'halt' and 'i' in the header file, in the
'private' section of the Form class.
This should be done as standard practice to avoid polluting the global
namespace. The variables in this case only need to have class scope.
Here are the files, both source and header that I have modified:
// The .cpp file
//--------------------------------------------------------------------------
-
#include <vcl.h>
#pragma hdrstop
#include "Mac.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
halt = false;
i = 0;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Increase();
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Application->MessageBox("Do you wish to continue?", "Alert!",
MB_YESNO) == IDNO)
halt = true;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Application->Terminate();
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Increase(void)
{
halt = false;
do {
Edit1->Text = ++i;
Edit1->Update();
Application->ProcessMessages();
} while(!halt);
}
//--------------------------------------------------------------------------
-
// The header file
//--------------------------------------------------------------------------
-
#ifndef MacH
#define MacH
//--------------------------------------------------------------------------
-
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//--------------------------------------------------------------------------
-
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TEdit *Edit1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
bool halt;
int i;
void __fastcall Increase(void);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//--------------------------------------------------------------------------
-
extern PACKAGE TForm1 *Form1;
//--------------------------------------------------------------------------
-
#endif
HTH
XXXX@XXXXX.COM
Remove the obvious to reply.