Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: FindComponent and dynamically added TMenuItems



Permlink Replies: 2 - Last Post: Mar 5, 2018 12:10 PM Last Post By: Jean Cremers
Jean Cremers

Posts: 15
Registered: 11/19/10
FindComponent and dynamically added TMenuItems
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 5, 2018 8:49 AM
Hi Forum

I make a bunch of TMenuItems dynamically with NewItem(), and i assign each to an existing TMenuItem. So they become a component on the form, at least that's what i thought. Each gets a unique name.
But i can't find them later on using FindComponent, only the designtime added TMenuItems show up.
Can't I use FindComponent on dynamically added TMenuItems?

Thanks.

Edited by: Jean Cremers on Mar 5, 2018 8:51 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: FindComponent and dynamically added TMenuItems [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 5, 2018 10:50 AM   in response to: Jean Cremers in response to: Jean Cremers
Jean Cremers wrote:

I make a bunch of TMenuItems dynamically with NewItem(), and i assign
each to an existing TMenuItem. So they become a component on the
form, at least that's what i thought.

No, they are not members of the Form object. NewItem() creates a new
TMenuItem object with no Owner assigned to it.

Each gets a unique name. But i can't find them later on using
FindComponent, only the designtime added TMenuItems show up.

That means the dynamically created items are not owned by the
TComponent object that you are calling FindComponent() on (presumably
the TForm). Components created at design-time are always owned by the
Form/Frame/DataModule that they are placed on.

Can't I use FindComponent on dynamically added TMenuItems?

If they have no Owner assigned, then no. Why not just store them in
your own T(Object)List instead? Then you don't have to hunt for them.

Otherwise, you will need to assign the TForm as their Owner, eg:

TMenuItem *item = NewItem(...);
MyForm->InsertComponent(item); // <-- assign ownership
...


--
Remy Lebeau (TeamB)
Jean Cremers

Posts: 15
Registered: 11/19/10
Re: FindComponent and dynamically added TMenuItems [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 5, 2018 12:10 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy,

I better maintain my own list indeed!
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02