July Elizabeth
July Elizabeth

Reputation: 159

ProgressBar on TForm2 of C++ Builder

On the TForm2, I'm trying to make a TProgressBar that starts at 0%, and takes 30 seconds to get to 100%. The TProgressBar will begin to go up as soon as TCheckBox of TForm1 is checked.

I looked on Google, but that gave me nothing good on this sort of thing.

Any advice?

TFORM1

//...

#include "Unit1.h"
#include "Unit2.h"

//---------------------------------------------------------------------------
void __fastcall TFormOne::MyCheckBoxClick(TObject *Sender)
{
  FormTwo->Show();
}

TFORM2

//...

#include "Unit2.h"
#include "Unit1.h"

//...

int MSecond = 0, MyTime = 0;

//---------------------------------------------------------------------------
__fastcall TFormTwo::TFormTwo(TComponent* Owner) : TForm(Owner)
{
  ProgressBar->Min = 0;
  ProgressBar->Max = 100;
  ProgressBar->Position = 0;
  Timer1->Enabled = true;
}

//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormCreate(TObject *Sender)
{
  MyTime = GetTickCount();
  MSecond = 0;
  Timer1->Enabled = false;
  ProgressBar->Position = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::Timer1Timer(TObject *Sender)
{
  MSecond = GetTickCount( ) - MyTime;
  if (MSecond < 30000)
    ProgressBar->Position = double Trunc(double(MSecond) / 300);
  else
  {
    ProgressBar->Position = 100;
    Timer1->Enabled = false;
  }
}

Upvotes: 0

Views: 2736

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595369

You did not implement TFormTwo correctly for what you are attempting to accomplish. It should look more like this instead:

class TFormTwo : class(TFormTwo)
{
__published:
    TProgressBar *ProgressBar;
    TTimer *Timer1;
    //...
    void __fastcall FormShow(TObject *Sender);
    void __fastcall FormHide(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall Timer1Timer(TObject *Sender);
    //...
private:
    DWORD StartTime;
    //...
public:
    __fastcall TFormTwo(TComponent* Owner);
};

__fastcall TFormTwo::TFormTwo(TComponent* Owner)
    : TForm(Owner)
{
    // you should set these at design-time instead
    ProgressBar->Min = 0;
    ProgressBar->Max = 100;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormShow(TObject *Sender)
{
    ProgressBar->Position = 0;
    StartTime = GetTickCount();
    Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormHide(TObject *Sender)
{
    Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::FormClose(TObject *Sender, TCloseAction &Action)
{
    Timer1->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormTwo::Timer1Timer(TObject *Sender)
{
    DWORD MSecond = GetTickCount() - StartTime;
    if (MSecond < 30000)
        ProgressBar->Position = int((double(MSecond) / 30000.0) * 100.0);
    else
    {
        ProgressBar->Position = 100;
        Timer1->Enabled = false;
    }
}

Upvotes: 2

Related Questions