RAD Studio VCL Reference
|
Indicates whether a given component is owned by the component.
function FindComponent(const AName: string): TComponent;
__fastcall TComponent * FindComponent(const AnsiString AName);
FindComponent returns the component in the Components property array with the name that matches the string in the AName parameter. Use FindComponent to determine whether a given component is owned by another.
Component name matches are not case sensitive.
C++ Examples:
/* This code resizes the active control to twice as wide and half as high: */ void __fastcall TForm1::ButtonTSClick(TObject *Sender) { TRect MyRect; if (myActiveControl == NULL) exit; MyRect = myActiveControl->BoundsRect; MyRect.Right = MyRect.Left + (MyRect.Right - MyRect.Left) / 2; MyRect.Bottom = MyRect.Top + 2 * (MyRect.Bottom - MyRect.Top); myActiveControl->BoundsRect = MyRect; } void __fastcall TForm1::ButtonSFClick(TObject *Sender) { TRect MyRect; if (myActiveControl == NULL) exit; MyRect = myActiveControl->BoundsRect; MyRect.Right = MyRect.Left + 2 * (MyRect.Right - MyRect.Left); MyRect.Bottom = MyRect.Top + (MyRect.Bottom - MyRect.Top) / 2; myActiveControl->BoundsRect = MyRect; } void __fastcall TForm1::FormCreate(TObject *Sender) { TComponent *Temp; TListItem *ListItem; ListView1->ViewStyle = vsList; ListItem = ListView1->Items->Add(); ListItem->Caption = "Components: "; for (int I = ComponentCount - 1; I >= 0; I--) { Temp = Components[I]; ListItem = ListView1->Items->Add(); ListItem->Caption = Temp->Name; } } void __fastcall TForm1::ListView1SelectItem(TObject *Sender, TListItem *Item, bool Selected) { TComponent *comp = FindComponent(Item->Caption); if (dynamic_cast<TWinControl *>(comp) != NULL) myActiveControl = dynamic_cast<TWinControl *>(comp); }
/* The following example creates 20 edit boxes, using FindComponent with the edit box name to access each newly created edit box. */ const int EditBoxCount = 20; const int LeftCoordinate = 10; TEdit* pe[20]; void __fastcall TForm1::FormCreate(TObject *Sender) { const char* pszNamePrefix = "MyEdit"; for (int i=0;i<EditBoxCount;i++) { pe[i] = new TEdit(this); // These objects will be cleaned up by their owner (this). pe[i]->Name = pszNamePrefix + IntToStr(i+1); pe[i]->Left = LeftCoordinate; pe[i]->Top = i*EditBoxCount; pe[i]->Parent = this; } }
Delphi Examples:
{ This code resizes the active control to twice as wide and half as high: } procedure TForm1.ButtonSFClick(Sender: TObject); var MyRect: TRect; begin if (myActiveControl = nil) then exit; MyRect := myActiveControl.BoundsRect; MyRect.Right := MyRect.Left + (MyRect.Right - MyRect.Left) * 2; MyRect.Bottom := MyRect.Top + (MyRect.Bottom - MyRect.Top) div 2; myActiveControl.BoundsRect := MyRect; end; procedure TForm1.ButtonTSClick(Sender: TObject); var MyRect: TRect; begin if (myActiveControl = nil) then exit; MyRect := myActiveControl.BoundsRect; MyRect.Right := MyRect.Left + (MyRect.Right - MyRect.Left) div 2; MyRect.Bottom := MyRect.Top + (MyRect.Bottom - MyRect.Top) * 2; myActiveControl.BoundsRect := MyRect; end; procedure TForm1.FormCreate(Sender: TObject); var I: Integer; Temp: TComponent; ListItem: TListItem; begin ListView1.ViewStyle := vsList; ListItem := ListView1.Items.Add; ListItem.Caption := 'Components: '; for I := ComponentCount - 1 downto 0 do begin Temp := Components[I]; begin ListItem := ListView1.Items.Add; ListItem.Caption := Temp.Name; end; end; end; procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); var comp: TComponent; begin comp := FindComponent(Item.Caption); if comp is TWinControl then myActiveControl := TWinControl(comp); end;
{ The following example creates 20 edit boxes, using FindComponent with the edit box name to access each newly created edit box. } procedure TForm1.Button1Click(Sender: TObject); var i: Integer; const NamePrefix = 'MyEdit'; begin for i := 1 to 20 do begin TEdit.Create(Self).Name := NamePrefix + IntToStr(i); with TEdit(FindComponent(NamePrefix + IntToStr(i))) do begin Left := 10; Top := i * 20; Parent := self; end; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|