AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein GUI-Design mit VCL / FireMonkey / Common Controls .Controls und .Components nicht als Auflistung nutzbar?
Thema durchsuchen
Ansicht
Themen-Optionen

.Controls und .Components nicht als Auflistung nutzbar?

Ein Thema von freejay · begonnen am 11. Mai 2022 · letzter Beitrag vom 25. Mai 2022
Antwort Antwort
Medium

Registriert seit: 23. Jan 2008
3.688 Beiträge
 
Delphi 2007 Enterprise
 
#1

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 11. Mai 2022, 21:47
die Funktionalität wäre direkt in TComponent bzw. TWinControl implementiert.
Stellt sich die Frage: Wieso ist es das eigentlich nicht? Das ist doch ziemlich ein Paradebeispiel für solch ein Konstrukt. Bin ich auch schon kräftig drüber gestolpert, und war doch recht verwundert ob der Tatsachen.
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.391 Beiträge
 
Delphi 12 Athens
 
#2

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 11. Mai 2022, 21:57
für Components gibt es bereits einen Enumerator

for var C in IrgendeineTComponentInstanz do
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
freejay

Registriert seit: 26. Mai 2004
Ort: Nürnberg
273 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 12. Mai 2022, 08:52
Hallo zusammen,

sorry, konnte mir das gestern nicht mehr genau ansehen - war ein bisschen knapp an Zeit...

Wenn ich mir jetzt das mit
Code:
Panel1.ControlsOf<TButton>
ansehe, dann ist das natürlich

1. absolut objektorientiert und
2. mit der Angabe der Control-Typen, die man haben will, auch noch praktischer als eine reine Auflistung.

Das werde ich auf jeden Fall nutzen!

Vielen Dank dafür!

Gruß

Freejay
[Delphi 11.3.1 Enterprise; Win10/11; MySQL; VCL]
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.673 Beiträge
 
Delphi 12 Athens
 
#4

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 12. Mai 2022, 09:03
Stefan Glienke hat gestern noch ein wenig Hand angelegt und ich möchte euch das Ergebnis nicht vorenthalten:

Zitat von Stevie:
hab mal deinen control enumerator umgeschrieben, um jegliche allokationen zu vermeiden und alle methoden inlinen zu lassen
Delphi-Quellcode:
type
  TWinControlHelper = class helper for TWinControl
  type
    TControlEnumerator<T: TControl> = record
    private
      FIndex, FCount: Integer;
      FWinControl: TWinControl;
      FCurrent: T;
    public
      function MoveNext: Boolean; inline;
      property Current: T read FCurrent;
    end;

    TControls<T: TControl> = record
    private
      FWinControl: TWinControl;
    public
      function GetEnumerator: TControlEnumerator<T>; inline;
    end;
  public
    function ControlsOf<T: TControl>: TControls<T>; inline;
  end;

{ TWinControlHelper }

function TWinControlHelper.ControlsOf<T>: TControls<T>;
begin
  Result.FWinControl := Self;
end;

{ TWinControlHelper.TControls<T> }

function TWinControlHelper.TControls<T>.GetEnumerator: TControlEnumerator<T>;
begin
  Result.FIndex := 0;
  Result.FWinControl := FWinControl;
  Result.FCount := FWinControl.ControlCount;
end;

function TWinControlHelper.TControlEnumerator<T>.MoveNext: Boolean;
var
  LControl: TControl;
begin
  repeat
    if FIndex < FCount then
    begin
      LControl := FWinControl.Controls[FIndex];
      Inc(FIndex);
      if LControl.InheritsFrom(T) then
      begin
        FCurrent := T(LControl);
        Exit(True);
      end;
    end
    else
      Exit(False)
  until False;
end;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
freejay

Registriert seit: 26. Mai 2004
Ort: Nürnberg
273 Beiträge
 
Delphi 11 Alexandria
 
#5

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 12. Mai 2022, 09:27
[Delphi 11.3.1 Enterprise; Win10/11; MySQL; VCL]
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.673 Beiträge
 
Delphi 12 Athens
 
#6

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 12. Mai 2022, 12:41
So, hier noch eine generische Alternative wenn es auf Performance nicht ganz so ankommt (wegen der anonymen Methode). Kann man dann allerdings auch leicht in anderen Situationen einsetzen.
Delphi-Quellcode:
type
  TEnumWrapper<T: class> = record
  type
    TGetItemFunc = TFunc<Integer, TObject>;
    TEnumerator = record
    private
      FIndex: Integer;
      FCount: Integer;
      FCurrent: T;
      FGetItem: TGetItemFunc;
    public
      function MoveNext: Boolean; inline;
      property Current: T read FCurrent;
    end;
  private
    FCount: Integer;
    FGetItem: TGetItemFunc;
  public
    constructor Create(ACount: Integer; AGetItem: TGetItemFunc);
    function GetEnumerator: TEnumerator; inline;
  end;

type
  TComponentHelper = class helper for TComponent
  public
    function ComponentsOf<T: TComponent>: TEnumWrapper<T>; inline;
  end;

type
  TWinControlHelper = class helper for TWinControl
  public
    function ControlsOf<T: TControl>: TEnumWrapper<T>; inline;
  end;

implementation

{ TEnumWrapper<T> }

constructor TEnumWrapper<T>.Create(ACount: Integer; AGetItem: TGetItemFunc);
begin
  FCount := ACount;
  FGetItem := AGetItem;
end;

function TEnumWrapper<T>.GetEnumerator: TEnumerator;
begin
  Result.FCount := FCount;
  Result.FGetItem := FGetItem;
  Result.FIndex := -1;
end;

function TEnumWrapper<T>.TEnumerator.MoveNext: Boolean;
var
  cmp: TObject;
begin
  repeat
    Inc(FIndex);
    if FIndex < FCount then
    begin
      cmp := FGetItem(FIndex);
      if cmp.InheritsFrom(T) then
      begin
        FCurrent := T(cmp);
        Exit(True);
      end;
      Continue;
    end;
  until True;
  Result := False;
end;

{ TComponentHelper }

function TComponentHelper.ComponentsOf<T>: TEnumWrapper<T>;
begin
  Result := TEnumWrapper<T>.Create(ComponentCount,
    function(Index: Integer): TObject
    begin
      Result := Components[Index];
    end);
end;

{ TWinControlHelper }

function TWinControlHelper.ControlsOf<T>: TEnumWrapper<T>;
begin
  Result := TEnumWrapper<T>.Create(ControlCount,
    function(Index: Integer): TObject
    begin
      Result := Controls[Index];
    end);
end;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Benmik

Registriert seit: 11. Apr 2009
570 Beiträge
 
Delphi 12 Athens
 
#7

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 13. Mai 2022, 18:35
Tolle Arbeit und toll, dass ihr euch die ganze Mühe macht, muss man mal sagen.
  Mit Zitat antworten Zitat
Heimlich

Registriert seit: 1. Apr 2020
11 Beiträge
 
Delphi 10.4 Sydney
 
#8

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 25. Mai 2022, 12:00
Stefan Glienke hat gestern noch ein wenig Hand angelegt und ich möchte euch das Ergebnis nicht vorenthalten:

Zitat von Stevie:
hab mal deinen control enumerator umgeschrieben, um jegliche allokationen zu vermeiden und alle methoden inlinen zu lassen
Delphi-Quellcode:
type
  TWinControlHelper = class helper for TWinControl
  type
    TControlEnumerator<T: TControl> = record
    private
      FIndex, FCount: Integer;
      FWinControl: TWinControl;
      FCurrent: T;
    public
      function MoveNext: Boolean; inline;
      property Current: T read FCurrent;
    end;

    TControls<T: TControl> = record
    private
      FWinControl: TWinControl;
    public
      function GetEnumerator: TControlEnumerator<T>; inline;
    end;
  public
    function ControlsOf<T: TControl>: TControls<T>; inline;
  end;

{ TWinControlHelper }

function TWinControlHelper.ControlsOf<T>: TControls<T>;
begin
  Result.FWinControl := Self;
end;

{ TWinControlHelper.TControls<T> }

function TWinControlHelper.TControls<T>.GetEnumerator: TControlEnumerator<T>;
begin
  Result.FIndex := 0;
  Result.FWinControl := FWinControl;
  Result.FCount := FWinControl.ControlCount;
end;

function TWinControlHelper.TControlEnumerator<T>.MoveNext: Boolean;
var
  LControl: TControl;
begin
  repeat
    if FIndex < FCount then
    begin
      LControl := FWinControl.Controls[FIndex];
      Inc(FIndex);
      if LControl.InheritsFrom(T) then
      begin
        FCurrent := T(LControl);
        Exit(True);
      end;
    end
    else
      Exit(False)
  until False;
end;

Wie sieht es denn bei folgender Konstellation aus?

Delphi-Quellcode:
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];
Man bräuchte dann ja 3 Durchläufe, anstatt, wie im Beispiel zusehen, nur einen Durchlauf.
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.673 Beiträge
 
Delphi 12 Athens
 
#9

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 25. Mai 2022, 16:50
Das ist dann halt eine andere Anforderung, für die man ja immer noch den Standard-Enumerator verwenden kann:
Delphi-Quellcode:
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;
Eigentlich ist das aber ein Anwendungsfall für das Visitor Pattern:
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;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.673 Beiträge
 
Delphi 12 Athens
 
#10

AW: .Controls und .Components nicht als Auflistung nutzbar?

  Alt 11. Mai 2022, 23:04
Stellt sich die Frage: Wieso ist es das eigentlich nicht?
Ist es doch - zumindest teilweise:
Interessanterweise gibt es bereits einen Enumerator für Components, allerdings nicht generisch:
Delphi-Quellcode:
  for var cmp in Self do
    if cmp is TButton then
      TButton(cmp).Caption := 'Hello World';
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:37 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