Das ist meine procedure zum speichern
Code: Alles auswählen
procedure TForm2.SaveObjectList;
var
FileStream: TFileStream;
MyObject: TBlock;
I: integer;
LocationInt: integer;
begin
try
// Save the contents of the list to a file
FileStream := TFileStream.Create('mylist.dat', fmCreate);
try
for I := 0 to obList.Count - 1 do
begin
MyObject := obList.Items[I];
// Convert the Location property to an integer
LocationInt := MyObject.Location.X * 100 + MyObject.Location.Y;
// Write the values of MyObject to the file
FileStream.Write(MyObject.Id, SizeOf(MyObject.Id));
FileStream.Write(MyObject.Color, SizeOf(MyObject.Color));
FileStream.Write(LocationInt, SizeOf(LocationInt));
FileStream.Write(MyObject.Text[1], Length(MyObject.Text));
end;
finally
FileStream.Free;
end;
finally
end;
end;
Code: Alles auswählen
procedure TForm2.LoadObjectList(const filename: string; var list: TList);
var
FileStream: TFileStream;
MyObject: TBlock;
Id: integer;
Color: integer;
LocationInt: integer;
Location: TPoint;
//Text: ansistring;
Text: string;
TextSize: integer;
begin
// Load the list from the file
FileStream := TFileStream.Create(filename, fmOpenRead);
try
// Read the values of the objects from the file and add them to the list
while FileStream.Position < FileStream.Size do
begin
MyObject := TBlock.Create(Form2.Canvas);
// Read the values of MyObject from the file
FileStream.Read(Id, SizeOf(Id));
FileStream.Read(Color, SizeOf(Color));
FileStream.Read(LocationInt, SizeOf(LocationInt));
// Convert the Location property to an integer
// LocationInt := MyObject.Location.X * 100 + MyObject.Location.Y;
// Convert it back with this
Location.X := LocationInt div 100;
Location.Y := LocationInt mod 100;
// Read the Text from the file
FileStream.Read(TextSize, SizeOf(TextSize));
// FileStream.Read(Text, SizeOf(Text));
SetLength(Text, TextSize);
FileStream.Read(Text[1], TextSize);
// Set the properties of MyObject
MyObject.Id := Id;
MyObject.Color := Color;
MyObject.Text := Text;
MyObject.Location := Location;
// Add MyObject to the list
list.Add(MyObject);
end;
finally
FileStream.Free;
end;
end;
Ich glaube ich bin nicht weit von der Lösung entfernt. Aber bin gerade an einem Punkt wo ich keine Ahnung mehr hab, und auch nicht weiss nach was ich schauen muss. Es kann doch nicht so schwer sein die paar Properties wieder einem Objekt zu übergeben und vorher aus dem Stream auszulesen... Hat mir jemand bitte eine Lösung?