New Tip - ...find a component by it\'s name ?

  • From: "SwissDelphiCenter.ch" <newsletter@xxxxxxxxxxxxxxxxxxxx>
  • To: sdcnewtip@xxxxxxxxxxxxx
  • Date: Fri, 8 Mar 2002 09:38:00 +0100

* SwissDelphiCenter new Tip newsletter
* www.swissdelphicenter.ch
*
* To unsubscribe send a mail to sdcnewtip-request@xxxxxxxxxxxxx with the
* subject unsubscribe


Hallo,

Folgender neuer Tip ist neu auf SwissDelphiCenter verfügbar:

Besuchen Sie die Programmier Tips unter 
http://www.swissdelphicenter.ch/de/tipsindex.php


Bewerten Sie diesen Tip nach Schwierigkeitsgrad, Nützlichkeit und Gesamthaft auf
http://www.swissdelphicenter.ch/de/showcode.php?id=1048



Autor:
Thomas Stutz <tom@xxxxxxxxxxxxxxxxxxxx>
http://www.swissdelphicenter.ch


-------------
...eine Komponente über ihren Namen finden/ansprechen ?
-------------
Kategorie: VCL



{
  Use the forms FindComponent to find a component on the
  form. TypeCast the Result of FindComponent to the TComponent to be able to 
use it.
}

{Instead of writing / Anstatt dies zu schreiben:}

Edit1.Text := 'Text 1';
Edit2.Text := 'Text 2';
Edit3.Text := 'Text 3';
Edit4.Text := 'Text 4';
{...}
Edit10.Text := 'Text 10';

{It's easier to write / geht's so einfacher:}

for i := 1 to 10 do
  TEdit(FindComponent('Edit'+IntToStr(i))).Text := 'Text' + IntToStr(i);

{or/oder:}

for i:= 1 to 10 do
  (Findcomponent('Edit'+IntToStr(i)) as TEdit).Text := IntToStr(i);



{
  Another example: find a component on any form with
  a search string like this: 'MyForm10.Edit2'

  Ein anderes Beispiel:
  Eine Komponente auf irgendeiner Form finden mittels einer
  solchen Zeichenfolge: 'MyForm10.Edit2'
}

function FindComponentEx(const Name: string): TComponent;
var
  FormName: string;
  CompName: string;
  P: Integer;
  Found: Boolean;
  Form: TForm;
  I: Integer;
begin
  // Split up in a valid form and a valid component name
  P := Pos('.', Name);
  if P = 0 then
  begin
    raise Exception.Create('No valid form name given');
  end;
  FormName := Copy(Name, 1, P - 1);
  CompName := Copy(Name, P + 1, High(Integer));
  Found    := False;
  // find the form
  for I := 0 to Screen.FormCount - 1 do
  begin
    Form := Screen.Forms[I];
    // case insensitive comparing
    if AnsiSameText(Form.Name, FormName) then
    begin
      Found := True;
      Break;
    end;
  end;
  if Found then
  begin
    for I := 0 to Form.ComponentCount - 1 do
    begin
      Result := Form.Components[I];
      if AnsiSameText(Result.Name, CompName) then Exit;
    end;
  end;
  Result := nil;
end;

procedure TFrom1.Button1Click(Sender: TObject);
var
  C: TComponent;
begin
  C := FindComponentEx('MyForm10.Edit2');
  TEdit(C).Caption := 'Hello';
end;





Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatisch generierte EMail]


----------------------------------------------------
ENGLISH NEWSLETTER
----------------------------------------------------

Hi,

This new tip is new available on SwissDelphiCenter.ch:

Visit the programming tips at http://www.swissdelphicenter.ch/en/tipsindex.php


Rate this tip after skill, useful and overall at
http://www.swissdelphicenter.ch/en/showcode.php?id=1048



Author:
Thomas Stutz <tom@xxxxxxxxxxxxxxxxxxxx>
http://www.swissdelphicenter.ch


-------------
...find a component by it\'s name ?
-------------
Category: VCL



{
  Use the forms FindComponent to find a component on the
  form. TypeCast the Result of FindComponent to the TComponent to be able to 
use it.
}

{Instead of writing / Anstatt dies zu schreiben:}

Edit1.Text := 'Text 1';
Edit2.Text := 'Text 2';
Edit3.Text := 'Text 3';
Edit4.Text := 'Text 4';
{...}
Edit10.Text := 'Text 10';

{It's easier to write / geht's so einfacher:}

for i := 1 to 10 do
  TEdit(FindComponent('Edit'+IntToStr(i))).Text := 'Text' + IntToStr(i);

{or/oder:}

for i:= 1 to 10 do
  (Findcomponent('Edit'+IntToStr(i)) as TEdit).Text := IntToStr(i);



{
  Another example: find a component on any form with
  a search string like this: 'MyForm10.Edit2'

  Ein anderes Beispiel:
  Eine Komponente auf irgendeiner Form finden mittels einer
  solchen Zeichenfolge: 'MyForm10.Edit2'
}

function FindComponentEx(const Name: string): TComponent;
var
  FormName: string;
  CompName: string;
  P: Integer;
  Found: Boolean;
  Form: TForm;
  I: Integer;
begin
  // Split up in a valid form and a valid component name
  P := Pos('.', Name);
  if P = 0 then
  begin
    raise Exception.Create('No valid form name given');
  end;
  FormName := Copy(Name, 1, P - 1);
  CompName := Copy(Name, P + 1, High(Integer));
  Found    := False;
  // find the form
  for I := 0 to Screen.FormCount - 1 do
  begin
    Form := Screen.Forms[I];
    // case insensitive comparing
    if AnsiSameText(Form.Name, FormName) then
    begin
      Found := True;
      Break;
    end;
  end;
  if Found then
  begin
    for I := 0 to Form.ComponentCount - 1 do
    begin
      Result := Form.Components[I];
      if AnsiSameText(Result.Name, CompName) then Exit;
    end;
  end;
  Result := nil;
end;

procedure TFrom1.Button1Click(Sender: TObject);
var
  C: TComponent;
begin
  C := FindComponentEx('MyForm10.Edit2');
  TEdit(C).Caption := 'Hello';
end;




Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatic generated EMail]

Other related posts:

  • » New Tip - ...find a component by it\'s name ?