![]() |
AW: .Controls und .Components nicht als Auflistung nutzbar?
Zitat:
Wie sieht es denn bei folgender Konstellation aus?
Delphi-Quellcode:
Man bräuchte dann ja 3 Durchläufe, anstatt, wie im Beispiel zusehen, nur einen Durchlauf.
for i := 0 to ComponentCount-1 do
if Components[i] is TButton then TButton(Components[i]).Caption := 'Hello World' else if Components[i] is TPageControl then TPageControl(Components[i]).ActivePageIndex := 0 else if Components[i] is TEdit then TEdit(Components[i]).Font.Style := [fsBold]; |
AW: .Controls und .Components nicht als Auflistung nutzbar?
Das ist dann halt eine andere Anforderung, für die man ja immer noch den Standard-Enumerator verwenden kann:
Delphi-Quellcode:
Eigentlich ist das aber ein Anwendungsfall für das
for var cmp in Self do begin
if cmp is TButton then TButton(cmp).Caption := 'Hello World' else if cmp is TPageControl then TPageControl(cmp).ActivePageIndex := 0 else if cmp is TEdit then TEdit(cmp).Font.Style := [fsBold]; end; ![]()
Delphi-Quellcode:
type
TMyVisitor = class(TVisitor) { Source für TVisitor siehe Link } public procedure VisitButton(Instance: TButton); procedure VisitPageControl(Instance: TPageControl); procedure VisitEdit(Instance: TEdit); end; procedure TMyVisitor.VisitButton(Instance: TButton); begin Instance.Caption := 'Hello World'; end; procedure TMyVisitor.VisitEdit(Instance: TEdit); begin Instance.Font.Style := [fsBold]; end; procedure TMyVisitor.VisitPageControl(Instance: TPageControl); begin Instance.ActivePageIndex := 0; end; procedure TForm40.Button1Click(Sender: TObject); var visitor: TMyVisitor; begin visitor := TMyVisitor.Create; try for var cmp in Self do visitor.Visit(cmp); finally visitor.Free; end; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:16 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 by Thomas Breitkreuz