Solved

WriteComponent ReadComponent "Control has no parent"

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 104
Intraweb download file link ? 1 108
Dev Express grid collapse 2 39
Convert MS Word document to a PDF file 9 62
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

16 Experts available now in Live!

Get 1:1 Help Now