Solved

WriteComponent ReadComponent "Control has no parent"

Posted on 2001-08-08
3
734 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
Comment Utility
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
Comment Utility
Hi Igor,
thank you very much - great work!

Stefan Wolf
0
 

Author Comment

by:stefanwolf
Comment Utility
uups,
thanks again!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now