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
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
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

688 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