RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TClipboard.GetComponent Method

Retrieves a component from the clipboard.

Pascal
function GetComponent(Owner: TComponent; Parent: TComponent): TComponent;
C++
__fastcall TComponent * GetComponent(TComponent * Owner, TComponent * Parent);

Use GetComponent to retrieve a component that is stored on the clipboard. 

Owner becomes the owner of the retrieved component and Parent becomes its parent. Owner is usually a form, but both parameters can be nil (Delphi) or NULL (C++). 

Before a class is read from the clipboard, it must be registered by a call to RegisterClasses. If you try to read an unregistered class, you'll get an EClassNotFound exception.  

C++ Examples: 

 

/*
This example uses a button and a group box on a form. When
the user clicks the button, the button is copied to the
Clipboard and then retrieved from the Clipboard and placed
in the new parent of the button, the group box. The name of
the original button is changed to avoid having two components
with the same name at the same time.
*/
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
  // Register the TButton class so that the clipboard can
  // read and write button objects
   TMetaClass *MetaClass = __classid(TButton);
   RegisterClasses(&MetaClass, 0);
}

#include <Clipbrd.hpp>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TButton *groupButton;
  // copy the button to the clipboard
  Clipboard()->SetComponent(Button1);
  // rename the button which is still on the form
  Button1->Name = "OriginalButton";
  // Now retrieve the button from the clipboard

  // and add it to GroupBox1
  // Note that the clipboard button is named Button1,
  // while the source button has been renamed
  // to "OriginalButton"
  groupButton = (TButton *)Clipboard()->GetComponent(this, GroupBox1);
  groupButton->Top = Button1->Top + 30;
}

 

Delphi Examples: 

{
This example uses a button and a group box on a form. When
the user clicks the button, the button is copied to the
Clipboard and then retrieved from the Clipboard and placed
in the new parent of the button, the group box. The name of
the original button is changed to avoid having two components
with the same name at the same time.
} 
implementation

uses Clipbrd;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  groupButton : TButton;
begin
  { copy the button to the clipboard }
  Clipboard.SetComponent(Button1);
  { rename the button which is still on the form }
  Button1.Name := 'OriginalButton';
  { Now retrieve the button from the clipboard }
  { and add it to GroupBox1 }
  { Note that the clipboard button is named Button1, while }
  { the source button has been renamed to 'OriginalButton' }
  groupButton := TButton(Clipboard.GetComponent(Self, GroupBox1));
  groupButton.top := Button1.top + 30;
end;

initialization
  RegisterClasses([TButton]);{ registers the TButton class }

 

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