Link to home
Start Free TrialLog in
Avatar of LMuadDIb
LMuadDIbFlag for United States of America

asked on

TCollection Copy Item

hey all
I created this component which uses a TCollection, and it has alot of published properties but I want to be able to COPY a collection item instead of just ADDing an item and then adjusting all the properties manually from the delphi's Object Treeview/Object Inspecter. The defaults are fine, just the tweaking for each item can be a pain.

I want to add a menu item(s) which allows you to Add Item, Copy Item, Paste Item from the Object Treeview or when the component is right-clicked on a form. And I know I have to create a component editor to do that, but I have not been able to find any code that shows how I can actually "copy" or duplicate an item. Assign? I fooled around with it some, but got errors.

all the components ( statusbars, pagecontrols, etc...) I have seen only allow you to add/delete... nothing about copy/duplicate.

any help would be appreciated.
Paul
Avatar of LMuadDIb
LMuadDIb
Flag of United States of America image

ASKER

well, I kind of gotten what I needed working. Not exactly though
I will leave this question open...
maybe someone has a different way of doing it and I would be happen to pass the points over to him/them.
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
var
br:TWebSnagItem;  

3: // User selected "Copy Item"
    begin
    InputString:= InputBox('Copy Item', 'Input the index of the item you want to copy:', '0');
    InputIndex := strtoint(InputString);
      if InputIndex <= TWebSnag( Component ).Bars.Count then begin
        br := TWebSnag( Component ).Bars.Add;
        br.Assign(TWebSnag( Component ).Bars.Items[InputIndex]);
      end;
    Designer.Modified;
    end;

this works correctly if I apply the editor to a test component that uses a TCollection
if I apply the same code to my component I get the following error:
"Cannot assign a TWebSnagItem to a TWebSnagItem"

an item is added to the collection, but it uses the defaults... and does not copy any of the properties.
Now my TWebSnagItem does not have an "Assign" procedure, nothing is overriden,
but my test component did have an "Assign" procedure override.

 Do I have to override my compnents assign?
If I do then that would mean I have to manually change the assign code to match any changes in my additions/substractions to my properties later.
I guess the only way to get a TCollectionItem to be assigned to another TOurCollectionItem is to use the Assign method of our Collection class. It must be overriden like so:

procedure TCollectionItem.Assign(Source: TPersistent);
begin
  if Source is TCollectionItem then
    SomeProperty:= TCollectionItem(Source).SomeProperty
  else
    inherited; //raises an exception
end;

which means I have to adjust the Assign Proc everytime I make changes to my component.
thanx mikelittlewood :) I have a working TComponentEditor that can copy items at design time.
Paul
Sorry about late reply, been away.
Glad you got the issue solved though  :o)