![]() |
AW: .Controls und .Components nicht als Auflistung nutzbar?
Zitat:
Zitat:
|
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:
ansehe, dann ist das natürlich
Panel1.ControlsOf<TButton>
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 |
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:
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; |
AW: .Controls und .Components nicht als Auflistung nutzbar?
:thumb:
|
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; |
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:
|
AW: .Controls und .Components nicht als Auflistung nutzbar?
|
AW: .Controls und .Components nicht als Auflistung nutzbar?
Zitat:
|
AW: .Controls und .Components nicht als Auflistung nutzbar?
Zitat:
"remember me" funktioniert auch nicht... |
AW: .Controls und .Components nicht als Auflistung nutzbar?
Ich hatte das Problem noch nicht.
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:23 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