Forum > General

Writing And Reading a Components To From a Stream

(1/3) > >>

LBoxPO:
Hi everyone )

Here is a good example of how to write components to a stream and read them by recreating them on a form.
https://wiki.freepascal.org/Streaming_components
C:\lazarus\examples\componentstreaming

The problem is that the example involves only one object "AGroupBox" with one child object "ACheckBox", and studying the code I do not quite understand how to make all the components on the form be written to the stream, because in most programs the form will have many more components and different classes. Moreover, when I added the child component "TButton" to "AGroupBox" and changed

this code:
procedure TCompStreamDemoForm.OnFindClass(Reader: TReader;
const AClassName: string; var ComponentClass: TComponentClass);
begin
  if CompareText(AClassName,'TGroupBox')=0 then
    ComponentClass:=TGroupBox
  else if CompareText(AClassName,'TCheckBox')=0 then
    ComponentClass:=TCheckBox;
end;

to this:
procedure TCompStreamDemoForm.OnFindClass(Reader: TReader;
const AClassName: string; var ComponentClass: TComponentClass);
begin
  if CompareText(AClassName,'TGroupBox')=0 then
    ComponentClass:=TGroupBox
  else if CompareText(AClassName,'TCheckBox')=0 then
    ComponentClass:=TCheckBox
  else if CompareText(AClassName,'TButton')=0 then
    ComponentClass:=TButton;
end;

the "TButton" component I added to the stream was not recorded, that is, I was unable to add information about the second child object to the stream and only information about "AGroupBox" and its child object ACheckBox was recorded in the stream.

Please, Pascal experts, explain to me what I am doing wrong and what needs to be done so that any number of components with any number of child objects can be recorded in the stream and then recreated on the form?

As I understand it, a cycle will definitely be required here like this

// Conditional code
procedure WriteAllComponentsToStream(...)
var
  i: Integer;
  AStream: TMemoryStream; // Or TFileStream
begin
  for i := 0 to TForm.ComponentsCount - 1 do
  begin
    AppendWriteComponentToStream(AStream, TForm.Component);
  end;
end;

ASerge:
1. Use code tags for source.
2. The tools for reading and writing components uses a hierarchy through the owner, not the parent.

LBoxPO:

--- Quote from: ASerge on September 05, 2024, 09:20:39 am ---1. Use code tags for source.
2. The tools for reading and writing components uses a hierarchy through the owner, not the parent.

--- End quote ---

Thank you very much, it worked  8)
Your advice about owners helped and while studying this topic
I realized that it is impossible to assign an owner by writing
like this:
Button1.Owner := AGroupBox;

need to write like this
AGroupBox.InsertComponent(Button1);

As you can see in the picture, now everything is written and recreated correctly, but now an error occurs when closing the form  :(

I also did not quite understand your first advice about "Use code tags for source".
Can you give a link to examples on the topic of code tags?

Zvoni:

--- Quote from: LBoxPO on September 05, 2024, 11:35:06 am ---I also did not quite understand your first advice about "Use code tags for source".
Can you give a link to examples on the topic of code tags?

--- End quote ---

Code-Tags:
(remove the blank between [ and 'c')
[ code=pascal]
Some Pascal code
[/ code]
(remove the blank between / and 'c')

ASerge:

--- Quote from: LBoxPO on September 05, 2024, 11:35:06 am ---As you can see in the picture, now everything is written and recreated correctly, but now an error occurs when closing the form  :(

--- End quote ---
We need the full source code to see where the error is.

Navigation

[0] Message Index

[#] Next page

Go to full version