Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Referencing a TObject using a string

Posted on 2004-04-30
Last Modified: 2010-04-05
How do you reference an object (TMenuItem to be specific) in code through a string variable containing the object's name?

I need this because I want to to be setting the enable properties of the menu items in my application according to the data that will be supplied by a database.
Question by:ziche
LVL 27

Accepted Solution

kretzschmar earned 40 total points
ID: 10956966
you can use

  MenuItem : TMenuItem;
  menuitem := form1.findcomponent('MenuItemName') as TMenuItem;
  if assigned(MenuItem) then

meikl ;-)    

Expert Comment

ID: 10960106
Woudn't it be easier to use the Tag property ?

Assign your TMenuItems a unique Tag value, then browse through all the TMenuItem components (use the Component and ComponentCount properties of your TForm or TMenu and then filter by the ClassName property).


function TMenuItem TForm.FindMenuItem(Tag: Integer)
  Result := nil;
  for i = 1 to Form.ComponentCount
    if Form.Components[i].ClassName = "TMenuItem" then
      if Form.Components[i].Tag = LookupTag then
        Result := Form.Components[i];
        i := Form.ComponentCount;

Author Comment

ID: 10975799
Thanks Cayce for openig my mind and reminding me of the many different ways there are for approaching problems.

This time though, I'll go with  kretzschmar's solution because of its simpler implementation.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question