Board index » delphi » How to use TCollection / TCollectionItem

How to use TCollection / TCollectionItem

Hi,

I have an Component that uses TCollection and TCollectionItem.
And I want to Add an New Item in runtime mode.

The Component is called ContextBar.

I have tried like this:

ContextBar.Items.Add            

RESULT:
I get an New Item with DisplayText: "New Item"

I'd like to have an new Item with different Display Text like 'Hallo'.

I tried to change the Display text after using ADD but that didn't work.

How I can I create a new TCollectionItem with new properties?

thanx

Antonio

 

Re:How to use TCollection / TCollectionItem


In <3EBBABDB.3010...@stueber.de>, Antonio Cambule wrote:

Quote

> I have an Component that uses TCollection and TCollectionItem.
> And I want to Add an New Item in runtime mode.

> The Component is called ContextBar.

> I have tried like this:

> ContextBar.Items.Add              

> RESULT:
> I get an New Item with DisplayText: "New Item"

> I'd like to have an new Item with different Display Text like 'Hallo'.

Do you mean the DisplayName property?  As far as I can see, in D5,
TCollectionItem has no DisplayText property.

If you look at the implementation of the getter
(TCollectionItem.GetDisplayName) and setter
(TCollectionItem.SetDisplayName) methods for the DisplayName property,
you'll see several things:

- they are both virtual - which gives you a clue that they're designed
to be overrridden in descendent classes

- the getter simply returns the ClassName - so I'm at a bit of a loss
to understand how you're seeing something like 'New Item'?

- the setter ignores what you try to pass to it - effectively making it
a read-only property - although it calls Changed, which eventually
propagates into a call to TCollection.Update.

So here's how you can get DisplayName to behave a bit more usefully:

TMyCollectionItem = class(TCollectionItem)
private
  FMyDisplayName: string;
protected
  function GetDisplayName: string; override;
  procedure SetDisplayName(const Value: string); override;
public
  constructor Create(Collection: TCollection); override;
end;

......

function TMyCollectionItem.GetDisplayName: string;
begin
// Note there's no point in calling the inherited method
  Result := FMyDisplaName;
end;

procedure TMyCollectionItem.SetDisplayName(const Value: string);
begin
  if FMyDisplayName <> Value then
  begin
    FMyDisplayName := Value;
//  Calling inherited invokes Changed
    inherited;
  end;
end;

constructor TMyCollectionItem.Create(Collection: TCollection);
begin
  inherited;
  FDisplayName := 'MyCleverItem' + IntToStr(ID);
end;

--
Roger Morton
ro...@chez-morton.com

Re:How to use TCollection / TCollectionItem


Quote
In article <3EBBABDB.3010...@stueber.de>, Antonio Cambule wrote:
> I have an Component that uses TCollection and TCollectionItem.
> And I want to Add an New Item in runtime mode.

> The Component is called ContextBar.

> I have tried like this:

> ContextBar.Items.Add  

Add is a function that returns a reference to the newly created item. You
throw that away, and that is the root of your problem. Assign the return
value to a variable of the proper type and then use it to set the
properties you need.

--
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

Re:How to use TCollection / TCollectionItem


Quote
> I have tried like this:

> ContextBar.Items.Add

Try this:

with ContextBar.Items.Add do
begin
  DisplayName:='My Name';
end;

Sujit

Re:How to use TCollection / TCollectionItem


Quote
> Assign the return
> value to a variable of the proper type and then use it to set the
> properties you need.

Hi,

I have tried that like this:

Item := List.Items.Add;
Item.DisplayText := 'Hello';

That didn't work too.

Other Threads