Solved

WriteComponent ReadComponent "Control has no parent"

Posted on 2001-08-08
3
760 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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