Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi List index out of bounds (10) (https://www.delphipraxis.net/55160-list-index-out-bounds-10-a.html)

lucius 17. Okt 2005 16:03


List index out of bounds (10)
 
Hallo Leute, ich hab ne Save und Load Function fuer eine Listview, das speichern geht gut aber wenn ich die File wieder im Listview laden moechte und Sie mehr als 10 Eintraege hat kriege ich ein List index out of bounds error.
Kann mir da jemand bitte helfen?
Dank im voraus.

Delphi-Quellcode:
procedure TMainForm.LoadAsActionExecute(Sender: TObject);
begin
  with OpenDialog do
  begin
  If Execute then
    begin
    ListView.Items.Clear;
    LoadListViewToFile(ListView, FileName);
    end
  else
    exit;
  end;
end;

sakura 17. Okt 2005 16:06

Re: List index out of bounds (10)
 
Dazu solltest Du mal den Code von DIr zeigen, wo der Fehler auftritt, da es sehr wahrscheinlich Dein Fehler ist ;) Nur ins Blaue zu raten bringt hier nicht viel.

...:cat:...

P.S.: In der Zwischenzwit hast Du Code gepostet :mrgreen: Aber interessant wäre die Methode "LoadListViewToFile"

lucius 17. Okt 2005 16:10

Re: List index out of bounds (10)
 
Also hier den code zum laden der File.

Delphi-Quellcode:
procedure TMainForm.LoadListViewToFile(AListView: TListView; sFileName: string);
var
  F: TFileStream;
  IdxItem, IdxSubItem, IdxImage: Integer;
  W, ItemCount, SubCount: Word;
  pText: PChar;
  PTemp: PChar;
  MySignature: array [0..2] of Char;
  sExeName: string;
begin
  with AListView do
  begin
    ItemCount := 0;
    SubCount := 0;

    sExeName := ExtractFileName(sFileName);

    if not FileExists(sFileName) then
    begin
      MessageBox(Handle, PChar(Format(Msg1, [sExeName])), 'I/O Error', MB_ICONERROR);
      Exit;
    end;

    F := TFileStream.Create(sFileName, fmOpenRead);
    F.Read(MySignature, SizeOf(MySignature));

    if MySignature <> 'LVF' then
    begin
      MessageBox(Handle, PChar(Format(Msg2, [sExeName])), 'I/O Error', MB_ICONERROR);
      Exit;
    end;

    F.Read(ItemCount, SizeOf(ItemCount));
    Items.Clear;

    for idxItem := 1 to ItemCount do
    begin
      with Items.Add do
      begin
        //Read imageindex
        F.Read(SubCount, SizeOf(SubCount));
        //Read imageindex
        F.Read(IdxImage, SizeOf(IdxImage));
        ImageIndex := IdxImage;
        //Read the Caption
        F.Read(w, SizeOf(w));
        pText := StrAlloc(w + 1);
        pTemp := StrAlloc(w + 1);
        F.Read(pTemp^, W);
        StrLCopy(pText, pTemp, W);
        Caption := StrPas(pText);
        StrDispose(pTemp);
        StrDispose(pText);
        if SubCount > 0 then
        begin
          for idxSubItem := 1 to SubCount do
          begin
            F.Read(w, SizeOf(w));
            pText := StrAlloc(w + 1);
            pTemp := StrAlloc(w + 1);
            F.Read(pTemp^, W);
            StrLCopy(pText, pTemp, W);
            Items[idxItem - 1].SubItems.Add(StrPas(pText));
            StrDispose(pTemp);
            StrDispose(pText);
          end;
        end;
      end;
    end;

    F.Free;
  end;
end;
Der Fehler kommt sobald ich die File selektiert habe und Sie oeffnen moechte.

Kroko1999 17. Okt 2005 16:12

Re: List index out of bounds (10)
 
[Glaskugel]

du hast I vo0n 1 bis Count und
I geht von 0 bis Count-1

[/Glaskugel]


//EDIT1:
ICH kann hellsehen :dancer2:

sakura 17. Okt 2005 16:16

Re: List index out of bounds (10)
 
Zitat:

Zitat von Kroko1999
du hast I vo0n 1 bis Count und
I geht von 0 bis Count-1

Das ist es imo in diesem Fall nicht, weil da wo es darauf ankommt, arbeitet er mit "-1" ;-) Aber auf Anhieb sehe ich den Fehler auch nicht.

...:cat:...

lucius 17. Okt 2005 16:16

Re: List index out of bounds (10)
 
Noch vergessen zu erwaehnen es ist eine Virtuelle Listview.
Der Load code ansich funzt, bei der Listview gehts schief.

marabu 17. Okt 2005 17:24

Re: List index out of bounds (10)
 
Nur um den thread an dieser Stelle sauber abzuschließen - Lucius verwaltet seine Daten in einer StringList (NewsList), deshalb muss der Code zum Laden der ListView so aussehen:

Delphi-Quellcode:
procedure TMainForm.LoadActionExecute(Sender: TObject);
begin
  with OpenDialog do
    If Execute then
    begin
      NewsList.LoadFromFile(FileName);
      ListItems.Count := NewsList.Count;
      ListView.Invalidate;
    end
end;
Grüße vom marabu


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:30 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz