Autor Beitrag
Wolle92
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 27.07.07 08:26 
Hallo,
ich habe ein ListView, bei dem in der zweiten Spalte (also Columns[1]) die Größe der Dateien angezeigt wird. Allerdings formatiert, sodass dahinter auch noch B, KB, MB oder GB steht.
Mein Problem ist, dass auch Ordner angezeigt werden, und von denen nicht die Größe. Wenn ich die Dateien jetzt nach Größe sortieren will bekomme ich den Fehler
Zitat:
'' is not valid integer value!

Jetzt will ich das ganze über try..except abfangen, aber ich weiß nicht, was ich bei der except-anweisung hinschreiben soll... Kann mir das jemand sagen?
ausblenden 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:
//columntosort und columnclicks sind globale variablen
//und werden in OnColumnClick mit entsprechenden Werten belegt.
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
var ix: Integer;
begin
  if ColumnToSort = 0 then
    Compare := not (CompareText(Item1.Caption,Item2.Caption))
  else
  begin
    ix := ColumnToSort - 1;
    if ColumnToSort = 1 then
      try
        Compare := CompareValue(StrToInt(Copy(Item1.SubItems[ix],1,Pos(#20, Item1.SubItems[ix]))),
          StrToInt(Copy(Item2.SubItems[ix],1,Pos(#20, Item1.SubItems[ix]))));
      except
        on Exception do
          //Was soll hier hin?
      end
    else
      Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]);
  end;
  if columnclicks mod 2 = 0 then compare := not compare;
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 27.07.07 08:43 
Moin!

Ansatz: Pack die Größe in die .Data-Eigenschaft der ListItems, dann kannst du diese als Sortierkriterium verwenden, ohne immer wieder umzurechnen. :idea: Hmm... Moment... hier ist das mal als Beispiel drin. :les: ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 27.07.07 09:56 
das ist sowieso ne gute Idee... aber meine eigentlich frage war ja, wie ich das mit den Ordner gebacken kriege
nen Ofen einscannen hilft da nicht...
Klabautermann
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Veteran
Beiträge: 6366
Erhaltene Danke: 60

Windows 7, Ubuntu
Delphi 7 Prof.
BeitragVerfasst: Fr 27.07.07 10:01 
Hallo,

eigendlich solltest du dein Ziel doch erreichen können wenn du anstelle von StrToInt ein StrToIntDef benutzt. Da kannst du dann einfach einen Default wert (am besten wohl -1 oder so) für den Fall angeben, dass die Konvertierung scheitert.

So hätten dann deine Ordner die kleinste größe und würden nach oben sortiert werden.

Gruß
Klabautermann
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 27.07.07 10:04 
Moin!

user profile iconWolle92 hat folgendes geschrieben:
das ist sowieso ne gute Idee... aber meine eigentlich frage war ja, wie ich das mit den Ordner gebacken kriege

Wenn du die Größe beim Anlegen der LsitItems in das .Data-Property packst, stellt sich das Problem doch gar nicht... :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 27.07.07 10:10 
hab ich auch grad gemerkt...