[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

WriteComponent ReadComponent "Control has no parent"

Posted on 2001-08-08
3
Medium Priority
?
819 Views
Last Modified: 2007-12-19
WriteComponent/ReadComponent can't be used with a Listbox ?

procedure TForm1.Button3Click(Sender: TObject);
var FileStream: TFileStream;
begin
  FileStream:= TFileStream.Create( 'c:\temp\listbox.dta', fmCreate );
  FileStream.WriteComponent( LISTBOX1 );
  FileStream.Free;
end;

procedure TForm1.Button4Click(Sender: TObject);
var FileStream: TFileStream;
begin
  FileStream:= TFileStream.Create( 'c:\temp\listbox.dta', fmOpenRead );
  FileStream.ReadComponent( nil );
  FileStream.Free;
end;

Using the same ReadComponent-Code in the FormCreate-Event works, see VCL-Code (csReading..):

procedure TWinControl.CreateWnd;
...
if (Owner <> nil) and (csReading in Owner.ComponentState) and...
0
Comment
Question by:stefanwolf
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 400 total points
ID: 6364517
Hi stefanwolf,

I guess what you mean. Seems you'v got some messages about "has not parent window" or "not registered". Here is a sample how to write and read TListBox to/from the stream. But first of all you should register class of component (see initialization section of this sample).

-----
Igor.


procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  FileStream: TFileStream;
  W: TWriter;
begin
  FileStream:= TFileStream.Create( 'd:\listbox.dta', fmCreate );

  W := TWriter.Create(FileStream, 1024);
  W.WriteComponent(Listbox1);
  W.Free;

  FileStream.Free;

  // just for testing to avoid of duplicate names
  Listbox1.Free;
end;

// this empty class is used to access protected method TReader.ReadData
type
  TMyReader = class(TReader)
  end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
var
  FileStream: TFileStream;
  R: TMyReader;
  C: TComponent;

begin
  FileStream:= TFileStream.Create( 'd:\listbox.dta', fmOpenRead );

  R := TMyReader.Create(FileStream, 1024);
  // create an instance of component
  C := TComponentClass(FindClass(R.ReadStr)).Create(nil);
  // assign parent
  TWinControl(C).Parent := Self;
  // read name of component
  C.Name := R.ReadStr;
  // read component's properties
  R.ReadData(C);

  R.Free;

  FileStream.Free;
end;

initialization
  RegisterClass(TListBox);

end.
0
 

Author Comment

by:stefanwolf
ID: 6366891
Hi Igor,
thank you very much - great work!

Stefan Wolf
0
 

Author Comment

by:stefanwolf
ID: 6366892
uups,
thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question