Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Suche Basisklasse, um Quellcode zu sparen (https://www.delphipraxis.net/101612-suche-basisklasse-um-quellcode-zu-sparen.html)

Christian18 16. Okt 2007 12:05


Suche Basisklasse, um Quellcode zu sparen
 
Hallo,

gibt es eine Art Basisklasse, damit ich nicht jedes Element extra ansprechen muss?

Delphi-Quellcode:
      // TGroupBox
      if Form.FindComponent(Node.Attributes['Name']) is TGroupBox then
        begin
          (Form.FindComponent(Node.Attributes['Name']) as TGroupBox).Caption:=Node.Attributes['Caption'];
        end;
      // TLabel
      if Form.FindComponent(Node.Attributes['Name']) is TLabel then
        begin
          (Form.FindComponent(Node.Attributes['Name']) as TLabel).Caption:=Node.Attributes['Caption'];
        end;
      // TBitBtn
      if Form.FindComponent(Node.Attributes['Name']) is TBitBtn then
        begin
          (Form.FindComponent(Node.Attributes['Name']) as TBitBtn).Caption:=Node.Attributes['Caption'];
        end;

DeddyH 16. Okt 2007 12:08

Re: Suche Basisklasse, um Quellcode zu sparen
 
Versuch es mal mit TControl.

Progman 16. Okt 2007 12:12

Re: Suche Basisklasse, um Quellcode zu sparen
 
Hinweis: Das Array Components enthält alle Komponenten, die sich auf einem Form befinden.
Delphi-Quellcode:
for i := 0 to Self.ComponentCount-1 do begin
  if (Components[i] is TLabel) then //machwas für Label
  if (Components[i] is TButton) then //machwas für Button
//  usw:
end;

dataspider 16. Okt 2007 13:36

Re: Suche Basisklasse, um Quellcode zu sparen
 
Zitat:

Zitat von DeddyH
Versuch es mal mit TControl.

Bei TControl ist Caption noch Protected.
Aber man kann ja eine Hilfsklasse erstellen:
Delphi-Quellcode:
THackControl = class(TControl)
published
  property Caption;
end;
Dann könnte man es so lösen:
Delphi-Quellcode:
Var
  AComponent: TComponent;
begin
  AComponent := Form.FindComponent(Node.Attributes['Name']);
  if Assigned(AComponent) then
    THackControl(AComponent).Caption := Node.Attributes['Caption'];
  ...
Frank


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:01 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz