Solved

WriteComponent ReadComponent "Control has no parent"

Posted on 2001-08-08
3
746 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 100 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 54
Delphi application Soap connection 5 104
Create Database on Android via Delphi dbExpress 3 92
Dev express lookupcombo 3 29
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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