3

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.

Warren P
  • 65,725
  • 40
  • 181
  • 316
XBasic3000
  • 3,418
  • 5
  • 46
  • 91

1 Answers1

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
  • 2
    The 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