[Lazarus] TStringList as published property of a component

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Thu Nov 18 11:38:36 CET 2010



On Thu, 18 Nov 2010, Alberto Manganaro wrote:

> Hi,
>
> first of all, as it's the first time I write here, I'd like to thank all the 
> people involved in the development/testing/mantaining/etc of Lazarus :-)
>
> I would like to understand better how to deal with the properties of a visual 
> component. My problem is the following: I'm writing a component that has a 
> property of class TStringList. This property is published, so I can correctly 
> access at design-time when I put my new component in a form. But if I assign 
> some values to the TStringList at design-time, it seems that at run time that 
> string list is empty.

Never use TStringList as a property type, always use TStrings.
(but initialize your field with a TStringList instance)

> Now, if I initialize that list through code (it means that I do this run 
> time, after the component is created) everything is fine. My doubt is that in 
> the constructor of the component the string list is created and initialized, 
> so maybe this is the problem. But if I don't create the string list in the 
> constructor of the component, of course I have an error.
>
> Can someone please explain me how properties of a component are managed at 
> design-time? How can I set my string list at design-time and correctly have 
> the values available at run-time?
>

The write must always happen through a setter procedure, and in the setter
you must assign the strings:

   Property MyProp : TStrings Read FMyProp Write SetMyProp;

Where SetMyProp is implemented as

Procedure TmYCOmponent.SetMyProp(AValue : TStrings);

begin
   if FMyProp=AValue then exit;
   FMyProp.Assign(AValue);
end;

Michael.

> Thanks!
>
> A.
>
> -- 
> Alberto Manganaro
>
> Milano Chemometrics and QSAR Research Group
> Department of Environmental Sciences
> University of Milano-Bicocca P.zza della Scienza, 1
> 20126 Milano - Italy
>
> http://michem.disat.unimib.it/chm/
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>




More information about the Lazarus mailing list