Re: ObjectTextToBinary
From: Peter Below (TeamB) (100113.1101_at_compuXXserve.com)
Date: 01/17/04
- Next message: Peter Below (TeamB): "Re: Interposer classes"
- Previous message: Didier Cabalé: "RTTI with a unknown class"
- In reply to: MeCeX: "ObjectTextToBinary"
- Next in thread: Robert Marquardt: "Re: ObjectTextToBinary"
- Reply: Robert Marquardt: "Re: ObjectTextToBinary"
- Reply: MeCeX: "Re: ObjectTextToBinary"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 17 Jan 2004 21:21:43 +0100
In article <400945c1$1@newsgroups.borland.com>, MeCeX wrote:
> From: "MeCeX" <admin@mehmetcelik.com>
> Newsgroups: borland.public.delphi.language.objectpascal
> Subject: ObjectTextToBinary
> Date: Sat, 17 Jan 2004 16:25:06 +0200
>
> Found this example at delphi help
>
> function ComponentToString(Component: TComponent): string;
Note that for most purposes it is completely unnecessary to convert the stored
binary component to text. You can reload the component from the binary version
as well and save all the conversion overhead.
>
> i first convert an object using ComponentToString and after
> stringtocomponent
> also
> at initialization part of one unit ->
> registerclass(TButton)
> registerclass(TMemo)
>
> when i convert button1 string to TButton no error occured
> but when i convert memo1 string to TMemo "control memo1 has no parent
> window" exception occured at ReadComponent(nil) line. Why ?
Because some components are based on Windows controls and store their data not
in the component but in the associated Windows control. Do be able to do that
they need a window handle. To be able to create that they need to have a
Parent. If you want to load a component with this requirement from a stream it
is best to create the component first (empty), give it a parent, then load it
from the stream. Usually your technique works, however, since the VCL
components are typically smart enough to buffer the data internally until they
are able to create a window handle.
> I want to clone a form !!
The proper way to do that is to stream the complete form, not only components
on it. Then create an *empty* instance of the form class, using the CreateNew
constructor, and read the streams data into it.
function CloneForm( aForm: Tform ): TForm;
var
ms: TMemoryStream;
begin
Assert( Assigned( aForm ));
ms:= TMemoryStream.Create;
try
ms.WriteComponent( aForm );
ms.Position := 0;
Result := TFormclass( aForm.Classtype ).CreateNew( nil );
// Result := TFormclass( aForm.Classtype ).CreateNew( aForm.Owner );
ms.ReadComponent( Result );
finally
ms.Free;
end;
end;
Since the VCL streaming mechanism was developed for streaming forms this works
with much less problems than handling individual components. You do not even
need to register the component classes used on the form (unless you add new
components at run-time, those need to be registered) since the compiler
includes a class list as part of the form class record.
If you need a way to clone individual components search the newsgroup archives
for CloneComponent. It's not exactly trivial if you also want to clone event
handler assignments....
-- Peter Below (TeamB) Use the newsgroup archives : http://www.mers.com/searchsite.html http://www.tamaracka.com/search.htm http://groups.google.com http://www.prolix.be
- Next message: Peter Below (TeamB): "Re: Interposer classes"
- Previous message: Didier Cabalé: "RTTI with a unknown class"
- In reply to: MeCeX: "ObjectTextToBinary"
- Next in thread: Robert Marquardt: "Re: ObjectTextToBinary"
- Reply: Robert Marquardt: "Re: ObjectTextToBinary"
- Reply: MeCeX: "Re: ObjectTextToBinary"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]