Solved

Referencing a TObject using a string

Posted on 2004-04-30
3
158 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 40 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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