Dylan
Dylan

Reputation: 1183

How to save virtual listview immediately after it displays data?

I want to save data in virtual listview immediately after it is filled. If I use Savedialog to save data, it works. If I call save procedure in ListviewOnData event, it prompts Error " Can not create file...". I reproduce my observation here.

procedure TForm6.savefileList(AListView: TListView; sFileName: string);

begin
F:= TFileStream.Create(sFileName, fmCreate or fmOpenRead);
...                                                       
...
F.free;

end;

Procedure Tform6.SavevirtualistinSaveDialog;// A buttonClick calls this procedure
begin
If SaveDialog1.execute then
savefilelist(listview1, savedialog1.FileName);; // It works and save data in sfilename.
..
end;

procedure TForm6.ListView1Data(Sender: TObject; Item: TListItem);
begin


with ld do begin     
...
...
// filling data in virtual list

end;
             // Right after filling data, I call Dosavelist procedure.
Dosavelist; //error line " Can not Create file", If I annotate this line
            // and call SavevirtualistinDialog in another buttonClick event, 
            //the data can be saved.

end;

Procedure Tform6.Dosavelist;
begin
savefilelist(listview1, extractfilepath(application.exename)+'list.tmp');
end;

I expect save the data in listview (virtual mode) right after it is filled. But it prompts errors.

How to solve it. Thank you in advance.

New Edit:

I delete Dosavelist from listview1data event and put it in another function that tracks listview. It works. So it is solved. Thank you all for help.

//My complete savefilelist para is:

savefilelist(listview1, extractfilepath(application.exename)+'list.tmp'); 

//In SavevirtualistinSaveDialog procedure, the complete save code line is: 

savefilelist(listview1, savedialog1.FileName);

Or Dosavelist procedure may not be called there? Where? How can I capture the moment when the virtual listview has just finished being filled? I do not know what event that is right after ListviewData event?

Thank you again.

Upvotes: 0

Views: 774

Answers (1)

Uli Gerhardt
Uli Gerhardt

Reputation: 14001

This sounds as if you don't specify an absolute file path. Then Windows relies on the current working directory when saving the file. The save dialog sets this directory, so everything works. In your ListviewOnData event, the cwd probably isn't set correctly.

Upvotes: 1

Related Questions