Board index » cppbuilder » Design time and runtime

Design time and runtime

Hi guys. Which is the difference between creating a component at design
time (i.e. drag & drop it from the component palette) and at runtime
(i.e. newing it) ?

May I dynamically add a method to a form that has just been newed ? How
?

May I assign the dynamically created method to the OnClick handler of a
button that has just been newed upon the newed form ? How ?

All responses wellcome. Thanks.

PB

 

Re:Design time and runtime


Quote
PecosBill wrote:
> Hi guys. Which is the difference between creating a component at design
> time (i.e. drag & drop it from the component palette) and at runtime
> (i.e. newing it) ?

The difference is that at runtime the VCL automatically creates and deletes
the component, sets the component's properties by streaming them in from a
resource file, and sets up the component's events for you in the same
manner through the design time interface, as opposed to doing this all
yourself at run time. But once you have finished, the end result is
essentially the same.

Quote
> May I dynamically add a method to a form that has just been newed ? How
> ?

C++ does not have the concept of dynamically adding methods to forms at
runtime. It needs to be there before you compile.

Quote
> May I assign the dynamically created method to the OnClick handler of a
> button that has just been newed upon the newed form ? How ?

You can specify an OnClick handler as a method of your form before you
compile, and then at run-time you can create your form and assign that
method to OnClick. The method of course must have the correct signature to
be a particular handler and it must use the __fastcall calling convention.

Re:Design time and runtime


Quote
PecosBill wrote:

> Hi guys. Which is the difference between creating a component at design
> time (i.e. drag & drop it from the component palette) and at runtime
> (i.e. newing it) ?

> May I dynamically add a method to a form that has just been newed ? How
> ?

> May I assign the dynamically created method to the OnClick handler of a
> button that has just been newed upon the newed form ? How ?

> All responses wellcome. Thanks.

Here is an example with dynamic buttons.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TButton *Buttons[4];

  for(int i = 0;i < 4;i++)
  {
    Buttons[i] = new TButton(this); // this, being Form1, owns the
control
    Buttons[i]->Caption = "Button " + IntToStr(i);
    Buttons[i]->Left = 20;
    Buttons[i]->Top = Buttons[i]->Height * i;
    Buttons[i]->OnClick = MyOnClick; // make sure your arguments match
the event
    Buttons[i]->Parent = this; // parent can be form, panel, groupbox
etc.
  }

Quote
}

void __fastcall TForm1::MyOnClick(TObject *Sender)
{
  // Sender is object that called method
  TButton *Button = (TButton *)Sender; // cast Sender as TButton
  ShowMessage(Button->Caption);
Quote
}

Other Threads