I am importing a .DAT file into a ListView , so before the import is done the program looks for the expression {keyword}
in the items and that same expression is replaced by the text of an Edit .
Example:
Original item in .dat file : "The 7 wonders of {keyword}"
Edit Text : "Weight Loss"
Item imported into Listview : "The 7 Wonders of Weight Loss"
And in the same way with the other items.
How could I do this?
Code:
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ListView1Data(Sender: TObject; Item: TListItem);
procedure BtnLoadFromFileClick(Sender: TObject);
private
procedure LoadFromFile(AFileName: string);
public
{ Public declarations }
end;
TMyRecord = record
name: string;
address: string;
floatfield: Single;
integerfield: Integer;
end;
var
Form1: TForm1;
MyList: TList<TMyRecord>;
Code Continuation:
procedure TForm1.FormCreate(Sender: TObject);
begin
MyList := TList<TMyRecord>.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyList.Free;
end;
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := MyList[Item.Index].name;
Item.SubItems.Add(MyList[Item.Index].address);
Item.SubItems.Add(FloatToStr(MyList[Item.Index].floatfield));
Item.SubItems.Add(IntToStr(MyList[Item.Index].integerfield));
end;
procedure TForm1.LoadFromFile(AFileName: string);
var
MyFileStream: TFileStream;
MyBinaryReader: TBinaryReader;
temprecord: TMyRecord;
I, TempNumber: Integer;
begin
MyFileStream := TFileStream.Create(AFileName, fmOpenRead);
MyBinaryReader := TBinaryReader.Create(MyFileStream,
TEncoding.Unicode, false);
MyList.Clear;
try
TempNumber := MyBinaryReader.ReadInteger;
for I := 0 to TempNumber - 1 do
begin
temprecord.name := MyBinaryReader.ReadString;
temprecord.address := MyBinaryReader.ReadString;
temprecord.floatfield := MyBinaryReader.ReadSingle;
temprecord.integerfield := MyBinaryReader.ReadInteger;
MyList.Add(temprecord);
end;
ListView1.Items.Count := MyList.Count;
MyBinaryReader.Close;
finally
MyBinaryReader.Free;
MyFileStream.Free;
end;
end;
procedure TForm1.BtnLoadFromFileClick(Sender: TObject);
begin
if OpenDialog1.Execute then
LoadFromFile(OpenDialog1.FileName);
end;
end.