Einzelnen Beitrag anzeigen

Benutzerbild von mschaefer
mschaefer

Registriert seit: 4. Feb 2003
Ort: Hannover
2.032 Beiträge
 
Delphi 12 Athens
 
#13

AW: Suche TMemo-Komponente mit Zeilenlänge > 1024

  Alt 5. Mär 2021, 16:44
Man könnte es auch in eine TListView einlesen

Delphi-Quellcode:

uses
  ComCtrls, Types, StrUtils;

procedure ListViewSaveToFile(ListView: TListView; const FileName: string);

  procedure AddTextToLine(var Line: string; const Text: string);
  begin
    Line := Line + Text + #9;
  end;

  procedure MoveCompletedLineToList(const Strings: TStringList; var Line: string);
  begin
    Strings.Add(System.Copy(Line, 1, Length(Line)-1));//remove trailing tab
    Line := '';
  end;

var
  Strings: TStringList;
  LatestLine: string;
  i, j: Integer;

begin
  LatestLine := '';

  Strings := TStringList.Create;
  try
    for i := 0 to ListView.Items.Count-1 do begin
      AddTextToLine(LatestLine, ListView.Items[i].Caption);
      for j := 0 to ListView.Items[i].SubItems.Count-1 do begin
        AddTextToLine(LatestLine, ListView.Items[i].SubItems[j]);
      end;
      MoveCompletedLineToList(Strings, LatestLine);
    end;
    Strings.SaveToFile(FileName, TEncoding.UTF8);
  finally
    Strings.Free;
  end;
end;

procedure ListViewLoadFromFile(ListView: TListView; const FileName: string);
var
  Strings: TStringList;
  i, j: Integer;
  Fields: TStringDynArray;
  Item: TListItem;
begin
  Strings := TStringList.Create;
  try
    Strings.LoadFromFile(FileName);
    ListView.Clear;
    for i := 0 to Strings.Count-1 do begin
      Fields := SplitString(Strings[i], #9);
      Item := ListView.Items.Add;
      Item.Caption := Fields[0];
      for j := 1 to high(Fields) do begin
        Item.SubItems.Add(Fields[j]);
      end;
    end;
  finally
    Strings.Free;
  end;
end;
Martin Schaefer
Phaeno
  Mit Zitat antworten Zitat