Recent

Author Topic: [SOLVED] Typed files Save and Read ListView  (Read 1343 times)

Pe3s

  • Full Member
  • ***
  • Posts: 101
[SOLVED] Typed files Save and Read ListView
« on: November 17, 2021, 08:16:47 pm »
Hello, How to complete the procedures to write and read data from a ListView?

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRecord = record
  3.     FirstName: string[100];
  4.     LastName: string[100];
  5.   end;
  6.  
  7. procedure TForm1.LoadData;
  8. begin
  9.  if not FileExists('D:\Data.dat') then Exit;
  10.  data := TFileStream.Create('D:\Data.dat', fmOpenRead);
  11.  
  12.  data.Free;
  13. end;
  14.  
  15. procedure TForm1.SaveData;
  16. begin
  17.  data := TFileStream.Create('D:\Data.dat', fmCreate);
  18.  
  19.  
  20.  
  21.  data.Free;
  22. end;

« Last Edit: November 19, 2021, 08:22:54 pm by Pe3s »

wp

  • Hero Member
  • *****
  • Posts: 9173
Re: Save and Read ListView
« Reply #1 on: November 17, 2021, 10:31:53 pm »
Look at the attached project and try to understand it. I used a plain old text file for file access because it is easier to handle strings than with streams.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Pe3s

  • Full Member
  • ***
  • Posts: 101
Re: Typed files Save and Read ListView
« Reply #2 on: November 18, 2021, 02:05:09 pm »
Thank you, but I wanted to learn TFileStream and typed files.  I will continue to look for a solution.
And whether the use of XMLConig will be a good choice?
« Last Edit: November 18, 2021, 02:09:53 pm by Pe3s »

wp

  • Hero Member
  • *****
  • Posts: 9173
Re: Typed files Save and Read ListView
« Reply #3 on: November 18, 2021, 05:09:59 pm »
Did you edit the first post? IIRC there was no record definition yesterday... I wonder now what this type has to do with the TListView mentioned in the title? Do you mean the the Listview shows two columns, one with the FirstName, the other one with the LastName?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Pe3s

  • Full Member
  • ***
  • Posts: 101
Re: Typed files Save and Read ListView
« Reply #4 on: November 18, 2021, 05:17:31 pm »
@wp This is how I edited the post. I am trying to write and read data displayed in two columns, hence the definition of the record.

wp

  • Hero Member
  • *****
  • Posts: 9173
Re: Typed files Save and Read ListView
« Reply #5 on: November 18, 2021, 06:11:31 pm »
Here is another version of the demo project, now with filestream and the record type that you proposed.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Pe3s

  • Full Member
  • ***
  • Posts: 101
Re: Typed files Save and Read ListView
« Reply #6 on: November 18, 2021, 07:03:14 pm »
Thank you, I have one more question, I wrote a search engine, but I noticed that if there are several items with the same name, it marks the last thing I need to correct to search in turn
Code: Pascal  [Select][+][-]
  1. var i: Integer;
  2. begin
  3. for i := 0 to ListView1.Items.Count -1 do
  4. begin
  5.   if Pos(Edit5.Text, ListView1.Items[i].Caption) > 0 then
  6.   begin
  7.     ListView1.ItemIndex := i;
  8.     ListView1.Items[i].MakeVisible(false);
  9.     ListView1.SetFocus;
  10.     end;
  11.   end;

wp

  • Hero Member
  • *****
  • Posts: 9173
Re: Typed files Save and Read ListView
« Reply #7 on: November 18, 2021, 07:58:47 pm »
I'm not sure what you mean. Do you want to replace the duplicate strings? ListView.Items[i ].Caption is the text in the first column, ListView.Items[ i].SubItems[0] the text in the second column, etc.
Code: Pascal  [Select][+][-]
  1.  ListView1.Items[i].Caption := 'new text for 1st column';
  2.  ListView1.Items[i].SubItems[0] := 'new text for 2nd column';
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Pe3s

  • Full Member
  • ***
  • Posts: 101
Re: Typed files Save and Read ListView
« Reply #8 on: November 19, 2021, 01:56:30 pm »
I meant the ability to search for a record.  my code searches but kezeli there are several records with the same name, it selects the last one and not each one after pressing the button

 

TinyPortal © 2005-2018