Autor Beitrag
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: So 08.10.06 00:33 
Hi,

ich bin dabei eine Hintergrund-Klasse für meinen DFMeditor zu schreiben, nur leider habe ich bisschen Probleme mit der recursiven getNext-funktion...

Da der komplette Code bisschen viel ist, hab ich den mal hier coder.cycnus.de/entry.php?id=963 geposted.

TCompInfo stellt eine Komponente dar, hat 2 Listen (sub-Komponenten und Eigenschaften) und so entsteht ein Baum. Diesen will ich zur Anzeige rekursiv durchlaufen, um diese Struktur in einem Treeview darzustellen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
var Comp:TCompInfo;
    Prop:TPropInfo;
    tn:TTreenode;
    i:integer;
begin
  comp:=rootcomp;
  tn:=nil;
  while assigned(comp) do
  begin
    tn:=treeview1.items.Add(tn,comp.name+':'+comp.TypeName);
    for i:=0 to comp.Properties.Count-1 do
    begin
      prop:=comp.Properties.GetItem(i);
      Treeview1.Items.AddChild(tn,prop.Name+' = '+prop.Value);
    end;
    comp:=comp.GetNext;
  end;
end;


ich habe festgestellt das das "result:=FParent.GetNext;" falsch ist, da dort dann wieder mit item 0 angefangen wird (result:=FComponents.GetItem(0)) und somit eine Endlosschleife erzeugt wird. Wie könnte ich das lösen?

Gruß Frank
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Mo 09.10.06 13:01 
Du könntest es so lösen, dass du über den Inde gehst. Bei GetFirst ist ja der Index 0. Und dann nimmst du einfach, wenn i der Zähler ist
ausblenden Delphi-Quelltext
1:
2:
comp := objectlist.items[i+1];
inc(i);


Dann könntest du das lösen.

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 12.10.06 21:34 
Das Problem lässt sich folgendermaßen relativ einfach lösen:


  1. Man benötigt eine Variable Previous: TNodeObject, die den vorher zurückgegebenen Eintrag enthält.
  2. Ist Previous.HasChildren = true, so gibt man das erste Child von Previous zurück
  3. Man prüft, ob Previous.GetNextSibling <> nil ist.
  4. Wenn es keinen weiteren Zwilling gibt, geht man so lange eine Ebene nach oben, bis man einen Knoten findet, der einen weiteren Sibling besitzt.
  5. Erreicht man dabei den Root-Knoten, so gibt man NIL zurück.
  6. Immer das aktuelle Ergebnis der Funktion wird zusätzlich auch in Previous gespeichert.


HTH und ist auch in D3 machbar :P

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
_frank_ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Fr 13.10.06 16:06 
danke,
hab es hinbekommen...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function TCInfo.GetNext:TCInfo;
var c2:TListObj;
begin
  if hasChildren then
    result:=FComponents.GetItem(0)
  else
    result:=TCInfo(GetNextSibling);
  c2:=TCInfo(parent);
  while not assigned(result) and assigned(c2) do
  begin
    result:=TCInfo(c2.GetNextSibling);
    c2:=c2.parent;
  end;
end;


Gruß Frank