How can my Delphi component detect at design time if any other component is being dropped on the form?
-
3-1 You have a good answer, but in comments to it you are imposing lots of constraints not present in the question. That's always frustrating. I suggest you give more detail. Tell us the ultimate goal and maybe we can help. – David Heffernan Nov 03 '12 at 17:02
-
1-1. I agree with @David. This is an incomplete question based on the comments you've added to the answer below. If you have more specific requirements, they belong in the question itself and not in comments to an answer. The question should be complete itself, and not depend on wading through comments to determine what it is you're actually asking. – Ken White Feb 16 '13 at 02:41
-
Why have you still not edited your q to state the additional requirements mentioned in your comments? – MartynA Jul 14 '14 at 12:35
3 Answers
You should override Notification
method of your component; something like that:
type
TMyComponent = class(TComponent)
//..
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
end;
procedure TMyComponent.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opInsert) and (csDesigning in ComponentState) then begin
// AComponent was dropped on the form
end;
end;

- 27,213
- 5
- 67
- 118
-
Many thanks, this works well! However, it seems that at this time (Notification), the inserted component is not yet a child of the form. I.e. the inserted component is not yet included in TForm.ComponentCount. My component processes all the components on the form, so in this case the inserted component is not found in my processing. – user1580348 Nov 03 '12 at 14:46
-
I think the inserted component should already be in `TForm.Components` list when the form invokes `Notification` call. – kludg Nov 03 '12 at 15:05
-
AComponent.HasParent is false in Notification. Also AComponent.Name is empty and AComponent.DesignInfo is uninitialized in Notification. So there is not much one can do with AComponent in Notification. I think I will use a Timer which fires only once enabled in Notification to get things done. Or could there be an other solution? – user1580348 Nov 03 '12 at 16:48
-
-
Furthermore, this event is also fired at the time when the project is loaded, once for each component on the form! How could this be avoided? – user1580348 Nov 03 '12 at 16:59
-
@David Heffernan: Because that seems to be the only way to process the inserted component AFTER Notification, since its properties are not yet initialized in Notification. – user1580348 Nov 03 '12 at 17:03
-
2Nobody knows what you are trying to do. Read the question and see if you can find anything that talks about parenting, or about processing inserted components properties after initialization. We can't read your mind. Put yourself in our position. – David Heffernan Nov 03 '12 at 17:08
-
What I said is a very clear fact: AComponent properties are not yet available in Notification. And I need the properties AComponent.Name and AComponent.DesignInfo. – user1580348 Nov 03 '12 at 17:20
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19020/discussion-between-user1580348-and-david-heffernan) – user1580348 Nov 03 '12 at 17:34
If you mean controls being dropped instead of components, and if you mean dropping on your component rather than dropping on the form, then:
- Add an
CM_CONTROLLISTCHANGE
message handler to track controls before they are inserted, or - Add an
CM_CONTROLCHANGE
message handler to track controls after they are inserted (WParam
points to the control), - Don't do this until
csLoading
is out ofComponentState
to prevent tracking during form creation by the IDE.

- 43,011
- 8
- 105
- 200
-
Thanks for the answer. I need to track COMPONENTS (not controls) dropped on the form (not on my component). I need the properties of the dropped component to arrange the positions of non-visual components (which already works well). What I need is to do the processing (the arranging of the non-visual components) as soon as the user drops a new component on the form. For this I need the AComponent properties (which are not available in Notification). So the only solution seems to be a OnceTimer. – user1580348 Nov 03 '12 at 17:46
Just a tip, if it helps... i am having a similar issue:
- I want to hide non-visual component icon (for my
TMyLabel=class(Classes.TComponent)
component) when i drop it from IDE inside a Form, Panel, etc ...
I have oveloaded: Loaded
and ReadState
to get such icon out of sight (at design-time) ... on Loaded
& ReadState
i put DesignInfo
to point to (-100,-100), so icon is not shown
I have oveloaded: WriteState
to avoid Left & Top be saved to the .dfm (at design-time) ... on WriteState
i put it to point (0,0), so it is not saved inside the .dfm
Note: I use same technique/trick to not save properties i do not want, etc ... i really only let Caption to be saved inside the .dfm
The question / tip is:
- When i drop a new "control" (of my component) into the form, such icon is visible just where i drop it... how to hide it?
Maybe user1580348
is trying something similar ... or something related to auto-align such non-visual "controls" / "components".
In other words:
- How can we control the icon position when dropping a new (our component) control on a form, panel, etc...
I know my problem is much easier, but i did not yet solve it... i want just to hide that icon IDE shows for non-visual components (only for controls that are of my component)... but maybe knowing how to it will also helps user1580348
.
As i said, it is just a tip/clue.

- 1
- 1