Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save/Load Listview to Text File??? 1

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
Hi, i have a listview with about 5 columns, and 30 rows.

can someone help me save this listview to text file so it ca be read easy, and then if the text file gets edited and i want to load the text file back into the listview, how could i do this?

Thanks!
 
There's no special trick to saving a listview to text. All you can do is browse the entire thing and save it to text. I suggest comma-delimited or tab-delimited format in your text file.

Code:
        AssignFile(outfile, SaveDialog1.FileName);
        Rewrite(outfile);
        writeln(outfile, '"', ListView1.Columns[0].Caption, '","',
                              ListView1.Columns[1].Caption, '","',
                              ListView1.Columns[2].Caption, '","',
                              ListView1.Columns[3].Caption, '","',
                              ListView1.Columns[4].Caption, '"');
        for i := 0 to (ListView1.Items.Count - 1) do
          begin
            A := ListView1.Items[i];
            writeln(outfile, '"', A.Caption, '","',
                            A.SubItems.Strings[0], '","',
                            A.SubItems.Strings[1], '","',
                            A.SubItems.Strings[2], '","',
                            A.SubItems.Strings[3], '"');
          end;
        CloseFile(outfile);

To load the data, you would have to parse the values and then add them to the listview.

Measurement is not management.
 
ok, i will see if i can retrieve the info back thanks:

Code:
var
  i: integer;
  a: tlistitem;
  outfile: textfile;
begin
  if SaveDialog1.Execute then
  begin
    Screen.Cursor:= crHourGlass;

    try
      AssignFile(outfile, SaveDialog1.FileName);
      Rewrite(outfile);
      writeln(outfile, '"',
      listview1.Columns[0].Caption, '","',
      listview1.Columns[1].Caption, '","',
      listview1.Columns[2].Caption, '","',
      listview1.Columns[3].Caption, '"', #13#10);

      for i := 0 to (listview1.Items.Count - 1) do
      begin
        A := listview1.Items[i];
        writeln(outfile, '"',
        A.Caption, '","',
        A.SubItems.Strings[0], '","',
        A.SubItems.Strings[1], '","',
        A.SubItems.Strings[2], '","');
      end;

      CloseFile(outfile);

      ShowMessage('Exported Table: ' + #13#10 + #13#10 + SaveDialog1.FileName);
    except on exception do
      ShowMessage('Could not save Table!');
    end;

    Screen.Cursor:= crDefault;
  end;
 
hi, do you have the code to load (read) the text file back, i cant seem to work it out how to do it.

thank you :)
 
Have you tried?

Code:
procedure TForm1.AddListView(x, y: integer; ListItem: TListItem; instr: string);
  var
    NewColumn: TListColumn;
  begin
    if x = 0 then
      begin
        NewColumn := Listview1.Columns.Add;
        NewColumn.Caption := instr;
      end
    else
      begin
        if y = 0 then
          ListItem.Caption := instr
        else
          ListItem.Subitems.Add(instr);
      end;
  end;

procedure TForm1.split_string(x: integer; instr: string);
  var
    partstr: string;
    reststr: string;
    y: integer;
    ListItem: TListItem;
  begin
    reststr := instr;
    y := 0;
    if x <> 0 then
      ListItem := ListView1.Items.Add;
    while Pos('",', reststr) <> 0 do
      begin
        partstr := Copy(reststr, 2, Pos('",', reststr)-2);
        AddListView(x, y, ListItem, partstr);
        inc(y);
        reststr := Copy(reststr, Length(PartStr) + 4, Length(reststr));
      end;
    partstr := Copy(reststr, 2, Length(reststr) - 2);
    AddListView(x, y, ListItem, partstr);
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  infile: TextFile;
  instr: string;
  x: integer;
begin
  if OpenDialog1.Execute then
    begin
      ListView1.ViewStyle := vsReport;
      AssignFile(infile, OpenDialog1.Filename);
      reset(infile);
      x := 0;
      while not eof(infile) do
        begin
          readln(infile, instr);
          split_string(x, instr);
          inc(x);
        end;
      closefile(infile);
      MessageDlg('CSV file loaded.', mtInformation, [mbOK], 0);
    end;
end;

Measurement is not management.
 
Hi Glenn, yes it seems to work thank you very much.

Im interested in your code, im not so good with I/O functions and this shows me how it works, although i dont understand it all :)

Assign, EOF i know somewhat, but then you need the logic to make it coding sense. I would not of come up with this myself, as i didnt think/know to use some of these routines.

Much appreciated, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top