Hier mal ein Beispiel zu einem simplen aber sehr effektiven Beispiel was man bei .net findet
http://www.codeproject.com/Articles/...ying-Recursion
Ein TreeView-Walker ... da hätte man auch selber mal drauf kommen können und hier der Delphi-Code dazu, der dem quasi 1:1 dem entspricht und auch so verwendet werden kann (der hier ist für FMX):
Delphi-Quellcode:
TProcessNodeEventArgs = class( TEventArgs )
private
FNode: TTreeViewItem;
FProcessDescendants: Boolean;
FProcessSiblings: Boolean;
FStopProcessing: Boolean;
public
constructor Create( ANode: TTreeViewItem );
property Node: TTreeViewItem read FNode;
property ProcessDescendants: Boolean read FProcessDescendants write FProcessDescendants;
property ProcessSiblings: Boolean read FProcessSiblings write FProcessSiblings;
property StopProcessing: Boolean read FStopProcessing write FStopProcessing;
end;
IProcessNodeEvent = IEvent<TProcessNodeEventArgs>;
ProcessNodeEvent = Event<TProcessNodeEventArgs>;
TTreeViewWalker = class( TComponent )
private
FTreeView: TTreeView;
FStopProcessing: Boolean;
FProcessNode: ProcessNodeEvent;
function GetProcessNode: IProcessNodeEvent;
function WalkNodes( ANode: TTreeViewItem ): Boolean;
public
constructor Create( ATreeView: TTreeView ); reintroduce;
procedure ProcessTree;
property ProcessNode: IProcessNodeEvent read GetProcessNode;
end;
{ TProcessNodeEventArgs }
constructor TProcessNodeEventArgs.Create( ANode: TTreeViewItem );
begin
inherited Create;
FNode := ANode;
FProcessDescendants := True;
FProcessSiblings := True;
FStopProcessing := False;
end;
{ TTreeViewWalker }
constructor TTreeViewWalker.Create( ATreeView: TTreeView );
begin
if not Assigned( ATreeView )
then
raise EArgumentNilException.Create( 'ATreeView' );
inherited Create( ATreeView );
FTreeView := ATreeView;
end;
function TTreeViewWalker.GetProcessNode: IProcessNodeEvent;
begin
Result := FProcessNode;
end;
procedure TTreeViewWalker.ProcessTree;
var
LIdx: Integer;
begin
FStopProcessing := False;
for LIdx := 0 to FTreeView.Count - 1 do
if not WalkNodes( FTreeView.ItemByIndex( LIdx ) ) or FStopProcessing
then
Break;
end;
function TTreeViewWalker.WalkNodes( ANode: TTreeViewItem ): Boolean;
var
LArgs: TProcessNodeEventArgs;
LIdx: Integer;
begin
LArgs := TProcessNodeEventArgs.Create( ANode );
try
FProcessNode.Invoke( FTreeView, LArgs, False );
Result := LArgs.ProcessSiblings;
if LArgs.StopProcessing then
FStopProcessing := True
else begin
if LArgs.ProcessDescendants then
begin
for LIdx := 0 to ANode.Count - 1 do
begin
if not WalkNodes(ANode.Items[LIdx]) or FStopProcessing then
Break;
end;
end;
end;
finally
LArgs.Free;
end;
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9
dc 90 9d f0 e9 de 13 da 60)