RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TComponent.InsertComponent Method

Establishes the component as the owner of a specified component.

Pascal
procedure InsertComponent(AComponent: TComponent);
C++
__fastcall InsertComponent(TComponent * AComponent);

InsertComponent adds the component passed in the AComponent parameter to the end of the Components array property. The inserted component must have no name (no specified Name property value), or the name must be unique among all others in the Components list. 

When the owning component is destroyed, AComponent is destroyed also. 

Components are automatically inserted and removed when visually manipulating them in the form designer. Use InsertComponent when manually adding components to another Owner component's Components list.  

C++ Examples: 

 

/*
This code fragment moves any nonvisual components on the 
form into a separate data module. Note that the components 
are removed starting with the last component, so that the 
unprocessed portion of the Components array does not change.
Note: This code does not save the form or data module to disk
after the nonvisual components are moved.  If executed at 
runtime, the effect will not persist.
Add a second TForm and name it ComponentCount2.  Put 
"#include ComponentCount2.cpp" in ComponentCount.h.  
Place a TMemo in each form and a TButton in Form1.
*/

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  int I;
  TComponent *Temp;
  Memo1->Lines->Add("Components removed: ");
  Form2->Memo1->Lines->Add("Components added: ");
  for (I = ComponentCount - 1; I >= 0; I--)
  {
    Temp = Components[I];
    // only move components that are not controls
    if (dynamic_cast<TControl *>(Temp) == NULL)
    {
      RemoveComponent(Temp);
      Memo1->Lines->Add(Temp->Name);
      Form2->InsertComponent(Temp);
      Form2->Memo1->Lines->Add(Temp->Name);
    }
  }
}

 

Delphi Examples: 

{
This code fragment moves any nonvisual components on the 
form into a separate data module. Note that the components 
are removed starting with the last component, so that the 
unprocessed portion of the Components array does not change.
Note: This code does not save the form or data module to disk
after the nonvisual components are moved.  If executed at 
runtime, the effect will not persist.
Add a second TForm and name it ComponentCount2.  Put 
"ComponentCount2" at the end of the uses clause of Form1.  
Place a TMemo in each form and a TButton in Form1.
} 

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  Temp: TComponent;
begin
  Form1.Memo1.Lines.Add('Components removed: ');
  Form2.Memo1.Lines.Add('Components added: ');
  for I := ComponentCount - 1 downto 0 do
  begin
    Temp := Components[I];
    if not (Temp is TControl) then
    begin
      RemoveComponent(Temp);
      Form1.Memo1.Lines.Add(Temp.Name);
      Form2.InsertComponent(Temp);
      Form2.Memo1.Lines.Add(Temp.Name);
    end;
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!