1
I’m importing a file .DAT in a Listview, I need that before the import is done the program looks for the expression {keyword}
in the items and that the same expression is replaced by the text of a Edit.
Example:
Original item in file . dat: "7 {keyword wonders}"
Edit text: "Slimming"
Imported item in Listview: "The 7 Wonders of Slimming"
And in the same way with the other items.
How could I do that?
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>;
Continuation of the Code:
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.
Very good. Your code just gave the idea to use the String Replace function that worked the same way.
– lukkicode