[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Referencing a TObject using a string

Posted on 2004-04-30
3
Medium Priority
?
167 Views
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.
0
Comment
Question by:ziche
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 160 total points
ID: 10956966
you can use

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

meikl ;-)    
0
 
LVL 9

Expert Comment

by:Cayce
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).

PseudoCode:

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

Author Comment

by:ziche
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 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