How to write a simple component that never allows it to drop multiple time on the form?
let say, I already drop my TMyComponent on the form and if i will drop another TMyComponent it notifies 'Component already exist!' and will cancel the dropping of the component on the form.
Asked
Active
Viewed 185 times
3

Warren P
- 65,725
- 40
- 181
- 316

XBasic3000
- 3,418
- 5
- 46
- 91
-
Probably easier to block this at runtime. – David Heffernan Jan 10 '13 at 08:01
-
Are you trying to make a singleton? – Pieter B Jan 10 '13 at 08:47
-
Sounds like XB wants a "designtime singleton". – Warren P Jan 12 '13 at 20:10
1 Answers
10
The components Constructor takes an Owner
parameter: that'll normally be your Form (or DataModule). Simply walk the list of Owner.Components
and see if there's an other one of yours. If there is, raise an Exception.
This would block your component from being added, both at design time and at runtime.

Cosmin Prund
- 25,498
- 2
- 60
- 104
-
in hypothetical case `Owner` was not a form or frame - one can check `Owner.Owner` and so forth up the tree. – Arioch 'The Jan 10 '13 at 08:20
-
2The IDE always sets the Form (or DataModule) as the Owner of the components at design-time, so no need to go any further then Owner. Unless the test has some meaning for components created by the Developer at runtime. – Cosmin Prund Jan 10 '13 at 10:31