Delphi-PRAXiS
Seite 2 von 3     12 3      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   .Controls und .Components nicht als Auflistung nutzbar? (https://www.delphipraxis.net/210560-controls-und-components-nicht-als-auflistung-nutzbar.html)

Uwe Raabe 11. Mai 2022 23:04

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

Zitat von Medium (Beitrag 1505718)
Stellt sich die Frage: Wieso ist es das eigentlich nicht?

Ist es doch - zumindest teilweise:
Zitat:

Zitat von Uwe Raabe (Beitrag 1505707)
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';


freejay 12. Mai 2022 08:52

AW: .Controls und .Components nicht als Auflistung nutzbar?
 
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

Uwe Raabe 12. Mai 2022 09:03

AW: .Controls und .Components nicht als Auflistung nutzbar?
 
Stefan Glienke hat gestern noch ein wenig Hand angelegt und ich möchte euch das Ergebnis nicht vorenthalten:

Zitat:

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;

freejay 12. Mai 2022 09:27

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

Uwe Raabe 12. Mai 2022 12:41

AW: .Controls und .Components nicht als Auflistung nutzbar?
 
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;

Benmik 13. Mai 2022 18:35

AW: .Controls und .Components nicht als Auflistung nutzbar?
 
Tolle Arbeit und toll, dass ihr euch die ganze Mühe macht, muss man mal sagen. :thumb:

Uwe Raabe 16. Mai 2022 17:00

AW: .Controls und .Components nicht als Auflistung nutzbar?
 
https://quality.embarcadero.com/browse/RSP-38237

freejay 18. Mai 2022 07:45

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

Zitat von Uwe Raabe (Beitrag 1505905)

Super, danke!

Frickler 18. Mai 2022 08:32

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

Zitat von Uwe Raabe (Beitrag 1505905)

Ist das eigentlich normal, dass das Einloggen dort immer erst im 5. bis 10. Versuch klappt?

"remember me" funktioniert auch nicht...

freejay 18. Mai 2022 08:35

AW: .Controls und .Components nicht als Auflistung nutzbar?
 
Ich hatte das Problem noch nicht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:48 Uhr.
Seite 2 von 3     12 3      

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