Einzelnen Beitrag anzeigen

doemi

Registriert seit: 13. Dez 2005
16 Beiträge
 
#3

Re: Highscore - Problem mit sortieren !

  Alt 19. Feb 2006, 18:23
Hallo.

Hier der Code den ich verwende:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,Graphics,
  Controls, Forms, Dialogs, StdCtrls, ComCtrls, StrUtils, ExtCtrls;


type
  TForm1 = class(TForm)
    ListView1: TListView;
    btn1: TButton;
    edt1: TEdit;
    lbl2: TLabel;
    Label1: TLabel;
    Image1: TImage;
    OpenDialog1: TOpenDialog;
    procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
    procedure btn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure speichern;
    procedure laden;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Name: string;
  SaveList: TStrings;
  i, j: Integer;
  Zeit: Integer;

Const
  Delimitter = #14;

implementation


{$R *.dfm}

function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer;
var
  I,X: Integer;
  Len, LenSubStr: Integer;
begin
  if Offset = 1 then
    Result := Pos(SubStr, S)
  else
  begin
    I := Offset;
    LenSubStr := Length(SubStr);
    Len := Length(S) - LenSubStr + 1;
    while I <= Len do
    begin
      if S[I] = SubStr[1] then
      begin
        X := 1;
        while (X < LenSubStr) and (S[I + X] = SubStr[X + 1]) do
          Inc(X);
        if (X = LenSubStr) then
        begin
          Result := I;
          exit;
        end;
      end;
      Inc(I);
    end;
    Result := 0;
  end;
end;

procedure TForm1.laden;
var
  LoadList: TStrings;
  ActPos, PrevPos: Integer;
Begin
  If Not OpenDialog1.Execute Then Exit;
  LoadList := TStringList.create;

  Try
    // Laden...
    LoadList.LoadFromFile(OpenDialog1.FileName);
    // Lösche den ListView
    ListView1.Items.Clear;

    // Laufe durch Zeilen der StringList
    For i := 0 To pred(LoadList.Count) Do
    Begin
      PrevPos := 1;
      ActPos := 1;
      // Erste Position des Delimitters
      ActPos := PosEx(Delimitter, LoadList[i], succ(ActPos));
      // Erstelle Item
      With ListView1.Items.Add Do
      Begin
        // setze Caption des neuen Items
        Caption := Copy(LoadList[i], PrevPos, (ActPos - PrevPos));
        PrevPos := succ(ActPos);
        // erzeuge für jeden gefundenen Delimitter ein SubItem
        Repeat
          ActPos := PosEx(Delimitter, LoadList[i], succ(ActPos));
          If ActPos <> 0 Then
            SubItems.Add(Copy(LoadList[i], PrevPos, (ActPos - PrevPos)))
          Else
            SubItems.Add(Copy(LoadList[i], PrevPos, Length(LoadList[i])));
          PrevPos := succ(ActPos);
        // Wenn ActPos = 0 (kein Delimitter gefunden) -> fertig
        Until ActPos = 0;
      End;
    End;

  Finally
    // Freigeben...
    FreeAndNil(LoadList);
  End;
End;


procedure TForm1.speichern;
var
  SaveList: TStrings;
  i, j: Integer;
  TestVar: String;
Begin
  //If Not SaveDialog1.Execute Then Exit;
  SaveList := TStringList.create;
  Try
    // Dein ListView
    With ListView1 Do
    Begin
      // Laufe durch alle Items
      For i := 0 To pred(Items.Count) Do
      Begin
        TestVar := '';
        With Items[i] Do
          // Laufe durch alle SubItems
          For j := 0 To pred(SubItems.Count) Do
            TestVar := TestVar + Delimitter + SubItems[j];

        SaveList.Add(Items[i].Caption + TestVar);
      End;

    End;
    // Speichern
    SaveList.SaveToFile('domi.txt');
  Finally
    // Freigeben ...
    FreeAndNil(SaveList);
  End;
End;

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
begin
  if (Item1.Data = Item2.Data) then
    Compare := 0
  else if (Integer(Item1.Data) > Integer(Item2.Data)) then
    Compare := 1
  else
    Compare := -1;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  Name:=edt1.text;
  with ListView1.Items.Add do begin // neuen Eintrag erzeugen
    Caption := Name; // Name eintragen
    Zeit := Random(1000); // zufällige Zeit erzeugen
    SubItems.Add(IntToStr(Zeit)); // Zeit zum Anzeigen als Text
    Data := Pointer(Zeit); // Zeit zum Vergleichen binär speichern (.Data ist vom Typ Pointer, deshalb die typecasts)
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin;
  laden;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  speichern;
end;

end.

Mfg
doemi
  Mit Zitat antworten Zitat