Solved

TMainmenu at runtime

Posted on 1998-12-15
6
444 Views
Last Modified: 2010-04-04
Hey there here's my quesrtion,

I have created my own toolbar component that can create and remove buttons from a toolbar at runtime. That part works fine. The next step I need to produce is some sort of way to link the buttons produced to a procedure/function on the mainmenu ie a shortcut. I then will save this to an ini file for loading next time I come into the app. Is it possible to
actually capture the procedure at runtime(dynamically) and assign the procedure to the OnClick event of the Button on the ToolBar. A bugger of a question I know but there must some way of doing this without heading to Win API commands. I have thought of making my own TMainmenu with a few extra methods but will see if this can be done first.

Anyone got any ideas???

Regards Westy {:|
       
0
Comment
Question by:westy100697
  • 5
6 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 150 total points
ID: 1351208
You can do this using the RTTI information;

Assuming two components (Fred1 and Mary1) I can assign the onclick property from one to the other like this using the RTTI. This means that all you have to do is store the component and method names and you can assign it all using this technique.

var
 MaryPropInfo :  PPropInfo;
 FredPropInfo :  PPropInfo;

begin
  MaryPropInfo := GetPropInfo(Mary1.ClassInfo, 'OnClick');
  FredPropInfo := GetPropInfo(Fred1.ClassInfo, 'OnClick');
  SetMethodProp(Fred1, FredPropInfo, GetMethodProp(Mary1, MaryPropInfo));
end;

Cheers,

Raymond.

0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1351209
Of course, you say, this is the same as this:

Fred1.OnClick := Mary1.Onclick;  //!

However, if you do not know the method name in advance then its quite useful!

eg: Procedure CopyNamedEvent(Comp1, Comp2 : TComponent; Event : String);

var
     PropInfo1 :  PPropInfo;
     PropInfo2 :  PPropInfo;

    begin
      PropInfo1 := GetPropInfo(comp1.ClassInfo, Event);
      PropInfo2:= GetPropInfo(comp2.ClassInfo, Event);
      SetMethodProp(comp2, PropInfo2, GetMethodProp(Comp1, PropInfo1));
    end;

Cheers,

Raymond.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1351210
The TypInfo (and dsgnintf) units can make fascinating reading if you are interested in how to copy and manipulate properties in a generic fashion...

Cheers,

Raymond.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 1351211
Actually, you should also look at the FindComponent class method and the MethodAddress function - these together with the bits and bobs outlined above should do the job...

If you need any more pointers just yell :-)

Cheers,

Raymond.
0
 

Author Comment

by:westy100697
ID: 1351212
rwilson,
           Gee this is becoming a habit you answering my questions...good habit though. I had a read through what you put here and was not quite clear on what you gave me. I then proceeded to put it into action and you little ripper it works. How did you happen to come across this? Both those units you told me to have a look at are very, very good reading. Handy to know eh. I was about to ask you a little extra question but I answered that myself whilst typing so I will give you the points and a big pat on the back. Thanks a lot
rwilson. Excellent Answer.

WESTY :)
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1351213
Westy,

I'm glad to help! I got into this stuff when D2 was new working with probably the first form designer external to the Delphi one that was developed (fedit2). Its very interesting stuff and has pretty awesome applications when you think about what you can do with it.

Cheers,

Raymond.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 161
Working with hours 3 65
Multiple image collision 13 81
can't find the executable in Simulator 1 101
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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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