Re: Adding "Items" property to component

From: Bob Richardson (bobr)
Date: 12/31/04


Date: Thu, 30 Dec 2004 17:07:37 -0800

The first time, I tried to change an existing component. That's when I got
the abstract error. Next I tried to descend from an existing
component...EXACTLY the way you suggested, and it worked perfectly. I'll
probably find some silly error with my first attempt when I dig into it
deeper. Thanks for your help Rob.

"Bob Richardson" <bobr at whidbey dot com> wrote in message
news:2N2dnSncjNBkBkncRVn-qQ@whidbeytel.com...
> After making these changes, I installed the component - then dropped in on
> a form. When I tried to add some values to "Items" in the Object
> Inspector, I got "Abstract Error" Have an idea what might be wrong?
>
>
> "Rob Kennedy" <me3@privacy.net> wrote in message
> news:33hffjF40g8keU1@individual.net...
>> Bob Richardson wrote:
>>> I've created a descendent of TCustomPanel and I'd like to add a property
>>> "Items" just like the Items property of TListBox and some of the other
>>> VCL components. Can someone point me in the right direction. Thank you.
>>
>> TListBox.Items is a TStrings object. All you need to do is declare a
>> TStrings property in your component and attach it to property accessor
>> functions. You'll also need a TStrings field in your class.
>>
>> type
>> TStringHolder = class(TComponent)
>> private
>> FItems: TStrings;
>> procedure SetItems(const Value: TStrings);
>> public
>> constructor Create(AOwner: TComponent); override;
>> destructor Destroy; override;
>> published
>> property Items: TStrings read FItems write SetItems;
>> end;
>>
>> constructor TStringHolder.Create(AOwner: TComponent);
>> begin
>> inherited;
>> FItems := TStringList.Create;
>> end;
>>
>> destructor TStringHolder.Destroy;
>> begin
>> FItems.Free;
>> inherited;
>> end;
>>
>> procedure TStringHolder.SetItems(const Value: TStrings);
>> begin
>> FItems.Assign(Value);
>> end;
>>
>> The above should be all you need to have a simple component that you
>> could put on a form and hold an arbitrary list of strings. (Storing the
>> strings in a linked-in resource file would be better, if that's all you
>> need.) Since FItems is actually a reference to a TStringList object, you
>> can assign event handlers to its OnChange and OnChangin properties, and
>> then your component will be notified whenever someone changes the
>> contents of the list.
>>
>> --
>> Rob
>
>