TStrings property in custom component

Hi

I am writing my first visual component, and struck against a problem with creating and using a property of type TStrings. Below are the relevant excerpts from my code.

type
  TMyMenu = class(TCustomControl)
  private
    FItems : TStrings;
    procedure SetItems(AItem : TStrings);

  published
    property Items : TStrings read FItems write SetItems;

implementation

procedure TMyMenu.SetItems(AItem : TStrings);
begin
     FItems := AItem;
end;


(I also found, quite by accident, a mention of array properties in the help files - but I couldn't get that to work either :(     )

I would appreciate any help with this, maybe an example or links to examples. Even better, a tutorial or how-to guide.

Thanks
sderaadtAsked:
Who is Participating?
 
interConnect With a Mentor Commented:
Dear sderaadt,

Sorry for misinterpreting your comment before


Thanks
Igor

0
 
interCommented:
Hi

If the problem is just using TStirngs, here are the guidlines, but if you are problem with deriving custom control please indicate what exactly you want:

1 - TStrings has just a wrapper to the strings allocated as TStringList. So it has no storage abitliy. But it is assignement compatible with TStringList which can store the strings.
2 - So you can declare your components private variable as FStrings : TStrings but to be able to store strings in it you should do
   FStrings := TStringList.Create;
Actually, if you have the VCL source code just look at the TListBox implementation and see how it stores the list of items in its TStrings field.
3 - The following example you gave is problematic.
    procedure TMyMenu.SetItems(AItem : TStrings);
    begin
         FItems := AItem;
    end;
If you do such thing you loose some memory since the items you have already created are not disposed off from the memory. Instead, the components and classes that store something has a method called Assign(..): The correct code for it is as follows:

    procedure TMyMenu.SetItems(AItem : TStrings);
    begin
         FItems.Assign(AItem);
    end;

4 - When  creating  a new component ALWAYS override the constructor and destructor even if you do not do anything in them

May these help
Regards,
Igor
0
 
ronit051397Commented:
Igor I right. Use TStringList instead of TStrings.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ZifNabCommented:
Well, nothing to be said anymore.... Everything is said. And now I've said my part too... C.U.
0
 
sderaadtAuthor Commented:
Thankyou inter

I've solved the problem, thanks to your tips.

You need to answer the question so you can get the points - or shall I delete the question?
0
 
interCommented:
Dear sderaadt,

If you lack of points, you may delete the question. No problem for me just notify to do so...

Sincerely,
Igor
0
 
sderaadtAuthor Commented:
inter

I have enough points and you deserve them, since you solved the problem :)

sderaadt
0
 
sderaadtAuthor Commented:
inter

Thats alright :)

Thanks for the tips.

sderaadt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.