0
votes

Writing my own Delphi VCL component inherited from TComponent with a bunch of properties, that must be unique to component's owner form. When I copy component from one form to another (with simple Ctrl+C, Ctrl-V) all properties are copied too. Any ideas on where (or how) I can handle copying or pasting the component on form and clear copied values (or set them to default)? For now I ended up with the idea of keeping component's owner form name (or other unique property) in the special component property and compare it with actual owner name in component's Loaded method. Maybe there is a more elegant or simpler way?

1
Don't think you can do this. Streaming doesn't distinguish that the data is going to the clipboard rather than any other destination.David Heffernan
Copy/paste means make a copy and paste it over here. If that's not what you want to happen, don't copy and paste. Drop a new component on the other form from the palette instead, which creates a new one with the default properties set. It doesn't make sense to try to copy without copying. If you put a completed form on a copy machine and press copy, you don't get a new blank form out of the machine, you get a copy of the already completed form. Why would you expect copying a component from one form to another to be any different? It's still a copy.Ken White
I believe OP is trying to control which properties are being copied to the new copy of the said component and which not. I'm not sure if this can even be done. By the way how does Delphi perform a task of copying a component? I'm guessing that first it needs to call default constructor of that component and after that it copies the properties from old component. So how is this done? If perhaps Delphi is relying on component's Assign method then I guess that by making custom Assign method might perhaps offer some control of which properties are being copied and which not.SilverWarior
@KenWhite Ok, is there a way to forbid component copy? To force user to drop a new component.zrocker
@SilverWarior you're right about constructor, but on Assign - no, Delphi not calling it at all. Delphi calling constructor, then somehow copying properties and then calls the other methods of the component (like Loaded).zrocker

1 Answers

0
votes

Found a solution myself. This is a kind of hack, but nonetheless it works.

First of all, when we copy the component, Delphi only copies the published properties - they are written in dfm file. It is more correct to say that Delphi will copy the implementation of the component in dfm format. You can easily verify this by copying the component and pasting it into Notepad. So now we can use the clipboard to analyze it in the newly pasted instance of our component and decide whether to clear the properties or not (or do something else).

A small example of such a check - a procedure that analyzes the values ​​in the clipboard for compliance with the current component:

function CheckClipboard:boolean;
begin
 try
  if (pos('object', Clipboard.AsText) <> 0)
    and (pos('object', Clipboard.AsText) < pos('TComponent', Clipboard.AsText))
    and (pos('TComponent', Clipboard.AsText) < pos(#13#10, Clipboard.AsText))
    and (pos(#13#10, Clipboard.AsText) < pos('end', Clipboard.AsText))
    and (TForm(Owner).Showing) then //This is for the function to not execute when the owner form is created or opened
       Result:=true
  else
    Result := false;
 except
  on E : Exception do
  begin
   MessageDlg('Clipboard error: '+E.Message, mtError, mbOKCancel, 0);
   Result := false;
  end;
 end;
end;

It returns true when the clipboard contains such a component, and false if not. I use it in Loaded method of my component.