Autor Beitrag
.#R4id
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 90

Windows XP Prof.
CodeGear Delphi 2007
BeitragVerfasst: Fr 07.12.07 19:12 
Hallo!

Weiß jemand wie man die TreeView Items nach einem Integer-Wert sortieren kann?
Der Integer wert ist in einem Record und das Record ist im jeweiligen Item gespeichert!

Beispiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
type
  TMyClass = ^MyClassData;
  MyClassData = Packed Record
    Index: Integer;
  end;

var
  MyClass: TMyClass;
begin
  New(MyClass);
  MyClass.Index := 1;
  TreeView1.Items.AddChildObject(NIL'Item 1', MyClass);

  New(MyClass);
  MyClass.Index := 3;
  TreeView1.Items.AddChildObject(NIL'Item 3', MyClass);

  New(MyClass);
  MyClass.Index := 2;
  TreeView1.Items.AddChildObject(NIL'Item 2', MyClass);
end;
Und das soll nun sortiert werden.

_________________
ausblenden Delphi-Quelltext
1:
if CopyAndPaste not avaible then Developer := Helpless;					
gispos
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 94

WIN 7
XE10, D2007
BeitragVerfasst: Fr 07.12.07 22:14 
Ich habs nicht probiert, aber Versuch dieses oder ähnliches per CustomSort oder
im OnSort-Event des TreeView. In SortMyClass muß evt. noch die Form1 geändert werden.
Die Bezeichnung deines Records entsprechen nicht den üblichen Standards, habe
mir erlaubt dies zu ändern.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
uses Math;
type
  PMyClassData= ^TMyClassData;
  TMyClassData = Packed Record
    Index: Integer;
  end;

function SortMyClass(P1: Integer; P2: Integer; LSort: Integer): Integer;
var My1,My2: PMyClassData;
begin
  My1:= PMyClassData(Form1.TreeView1.Items[P1].Data);
  My2:= PMyClassData(Form1.TreeView1.Items[P2].Data);
  Result:= CompareValue(My1.Index,My2.Index);
end;

procedure Sort();
begin
  TreeView1.Items[0].CustomSort(@SortMyClass,0,true);
end;


Nachtrag:
Also habs gerade mal probiert, und die obere Routine will nicht.
Im OnCompare-Event klappts dann aber. Der Aufruf: TreeView.Alphasort
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.TreeView1Compare(Sender: TObject; Node1, Node2: TTreeNode;
  Data: Integer; var Compare: Integer);
var My1,My2: PMyClassData;
begin
  My1:= PMyClassData(Node1.Data);
  My2:= PMyClassData(Node2.Data);
  Compare:= CompareValue(My1.Index,My2.Index);
end;


Warum fügst Du die items als Child ein? Warum nicht als normales item?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.Button2Click(Sender: TObject);
var MyClass: PMyClassData;
    i: Integer;
begin
  For i:= 1 to 5 do
  begin
    New(MyClass);
    MyClass.Index := i;
    TreeView1.Items.AddObject(nil'Item '+IntToStr(i), MyClass);
  end;
end;

Gib mal bescheid ob's klappt.
Gruß gispos
.#R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 90

Windows XP Prof.
CodeGear Delphi 2007
BeitragVerfasst: Fr 07.12.07 22:48 
Ich kann mir Gut vorstellen das es klappt, aber die Anzahl meiner Items ist nicht festgelegt :!:

Wie funktioniert das dann :?:

_________________
ausblenden Delphi-Quelltext
1:
if CopyAndPaste not avaible then Developer := Helpless;					
gispos
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 94

WIN 7
XE10, D2007
BeitragVerfasst: Sa 08.12.07 00:59 
Das ist egal! Die Sortierung funktioniert unabhängig davon. Du mußt nur darauf achten
daß auch in jedem Node ein Record vorhanden ist. Oder Du mußt im TreeView.OnCompare-Event
erst auf vorhandenen Record prüfen.

Gruß gispos
.#R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 90

Windows XP Prof.
CodeGear Delphi 2007
BeitragVerfasst: Sa 08.12.07 01:02 
Es ist auf jeden fall in jedem Item ein Record gespeichert. Aber es gibt 3 Verschiedene Records!

Record 1: TServer (Wird im komplette TreeView nur einmal erstellt)
Record 2: TChannel (Soll nach Integer-Wert sortiert werden)
Record 3: TPlayer (Soll nach Integer-Wert sortiert werden)

Wenn ich die funktion auf beide Records anwende, funktioniert das dann?

_________________
ausblenden Delphi-Quelltext
1:
if CopyAndPaste not avaible then Developer := Helpless;					
gispos
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 94

WIN 7
XE10, D2007
BeitragVerfasst: Sa 08.12.07 01:11 
Nein! Du mußt natürlich den Record zuerst ermitteln. Du kannst ja nicht einen XRecord einen
YRecord zuweisen! Darüber hab ich schon mal gepostet, am einfachsten ist es einen Master-Record zu verwenden.
www.delphi-forum.de/...78122&highlight=

Gruß gispos
.#R4id Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 90

Windows XP Prof.
CodeGear Delphi 2007
BeitragVerfasst: Sa 08.12.07 01:23 
Der Tipp hat wirklich was gebracht. Thema ist geklärt :!:

Danke user profile icongispos!

_________________
ausblenden Delphi-Quelltext
1:
if CopyAndPaste not avaible then Developer := Helpless;