How to run two forms in C++Builder

729 views Asked by At

It's a pleasure to share some information with you.

Excuse me, does anybody know how to have two FORMS running at the same time?

I mean, I execute a FOR clicking one BUTTON on FORM1 and set FOR's values to a LABEL on FORM2.

Here some codes in FORM1:

void __fastcall Form1::Button1Click(TObject *Sender){
  int i = 0;
  for (i=0;i<=10000;i++){
    Form1->Label1->Caption = i;
    Form2->Label1->Caption = i;  
  }
}

I just want to see this:

if.... Form1->Label1->Caption = 1, Form2->Label1->Caption has to be 1 too and so on. FORM2 shows me just the last result, which is 10,000.

I appreciate any help. Thank you !

1

There are 1 answers

7
manlio On BEST ANSWER

Just call Update(): (*)

void __fastcall Form1::Button1Click(TObject *Sender)
{
  for(int i = 0; i <= 10000; ++i)
  {
    Form1->Label1->Caption = i;
    Form2->Label1->Caption = i;

    Form1->Label1->Update();
    Form2->Label1->Update();
  }
}

Update():

Processes any pending paint messages immediately.

Call Update to force the control to be repainted before any more, possibly time-consuming, processing takes place. Use Update to provide immediate feedback to the user that cannot wait for the Windows paint message to arrive.

Update does not invalidate the control, but simply forces a repaint of any regions that have already been invalidated.

Application->ProcessMessages() will also work, but it's not the right choice: it interrupts the execution of an application so that it can process the message queue. ProcessMessages can be way slower.

(*) Changed since Remy's hint was absolutely better than the original answer (Update vs ProcessMessages)