Autor Beitrag
Radioactive
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: So 30.10.05 10:35 
Hi,

wenn ich ein Item in meinem ListView hinzufüge, tritt folgender Fehler auf: "Listenindex überschrietet das Maximum (2)". Ich füge das Item mit folgendem Code hinzu:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
//ListView1ColumnClick(Button1, ListView1.Columns.Items[0]);
MyItem := ListView1.Items.Add;
MyItem.Caption := NewUserForm.Edit1.Text;
MyItem.SubItems.Add(NewUserForm.MaskEdit1.Text);
MyItem.SubItems.Add([...]);
MyItem.SubItems.Add([...]);

Dabei tritt der Fehler in der markierten Zeile auf, aber nur, wenn ich das ListView nicht nach der ersten Spalte (ListView1.Caption) sortiert habe. Wenn ich das Programm über Delphi ausführe, tritt zusätzlich noch eine Zugriffsverletzung auf. Als art Notbehelf habe ich die (hier) auskommentierte Zeile hinzugefügt, die Ursache des Fehlers ist mir jedoch trotzdem schleierhaft.

Zum sortieren nach Spaltenklick benutze ich folgende Methoden:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
procedure TMainForm.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin
  ColumnToSort := Column.Index;
  Listview1.AlphaSort;
  Accending := not Accending;
end;

procedure TMainForm.ListView1Compare(Sender: TObject; Item1,
  Item2: TListItem; Data: Integer; var Compare: Integer);
begin
  case ColumnToSort of   
    0:   
    begin
      if Accending then
        Compare := CompareText(Item1.Caption, Item2.Caption)
      else
        Compare := CompareText(Item2.Caption, Item1.Caption);
    end;
    1:
    begin
      if Accending then
        Compare := CompareText(Item1.SubItems[0], Item2.SubItems[0])
      else
        Compare := CompareText(Item2.SubItems[0], Item1.SubItems[0]);
    end;
    2:
    begin
      if Accending then
        Compare := StrToInt(Item1.SubItems[1]) - StrToInt(Item2.SubItems[1])
      else
        Compare := StrToInt(Item2.SubItems[1]) - StrToInt(Item1.SubItems[1]);
    end;
    3:
    begin
      if Accending then
        Compare := StrToInt(Item1.SubItems[2]) - StrToInt(Item2.SubItems[2])
      else
        Compare := StrToInt(Item2.SubItems[2]) - StrToInt(Item1.SubItems[2]);
    end;
  end;
end;


Schonmal Danke im Vorraus,

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 30.10.05 11:27 
Hallo,

ist die Eigenschaft ListView.SortType auf stNone gesetzt?

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Radioactive Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: So 30.10.05 11:36 
Danke :D , jetzt geht alles einwandfrei, ich hatte nämlich stBoth eingestellt. Nun sortiert er zwar nicht mehr automatisch, aber der Eintrag wird ohne Fehler ganz unten hinzugefügt.

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: So 30.10.05 11:41 
Hallo,

dann ruf doch AlphaSort nach dem Einfügen der Items auf:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
MyItem.SubItems.Add(...
ColumnToSort := 0;
Accending := True;
Listview1.AlphaSort;
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Radioactive Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 179

Win 98, Win XP Home SP2
D3 Prof, D7 Pers, D2005 Pers
BeitragVerfasst: So 30.10.05 11:54 
Jo danke, habe ich schon ein paar Sekunden vor deinem Post eingefügt ;-):
ausblenden Delphi-Quelltext
1:
2:
3:
Accending := not Accending;
Listview1.AlphaSort;
Accending := not Accending;

Dadurch bleibt auch die zuletzt sortierte Spalte und die zuletzt benutze Sortierrichtung erhalten.

_________________
Radioactive
"Wer scrollt, verliert!" Matthias Stein, Informatiklehrer am GG