Solved

Loading actions at runtime

Posted on 2006-07-06
6
269 Views
Last Modified: 2010-04-05
Hi, All
I am building a component that uses prewritten actions. I searched but was unable to find a way to load actions at runtime. Can anyone post a simple example? I have already created a toolbar with buttons and images created at runtime. Thanks

Doug
0
Comment
Question by:dkeene
  • 3
6 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17053539
You must be talking about TActions?

Well, first, create your TActionList, then create a TAction for every action you want.
You MUST have an OnExecute method built to handle your actions, I would suggest
having one and assigning it to each of the TActions you create, then determine using the
Sender, what action to take when the OnExecute fires.

Then you want to assign the TAction that was created to the Action property of the
ToolbarBtn that you are creating.

Not that hard to do...

Here, I have two TButtons on a form and building actions for each.

var
  i: Integer;
begin
  for i := 1 to 2 do
  begin
    MyAction := TAction.Create(Self);
    MyAction.OnExecute := Action1Execute;
    MyAction.Enabled := True;
    MyAction.Caption := TButton(FindComponent('Button'+IntToStr(i))).Caption;
    TButton(FindComponent('Button'+IntToStr(i))).Action :=MyAction;
  end;

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17053542
No need to create the ActionList, however. I forgot to take that out before I posted.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 100 total points
ID: 17053553
A little better:

var
  i: Integer;
  Btn: TButton;
begin
  for i := 1 to 2 do
  begin
    Btn := TButton(FindComponent('Button'+IntToStr(i)));
    MyAction := TAction.Create(Btn);
    MyAction.OnExecute := Action1Execute;
    MyAction.Enabled := True;
    MyAction.Caption := Btn.Caption;
    Btn.Action :=MyAction;
  end;
end;
0
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 100 total points
ID: 17056749
>> I am building a component that uses prewritten actions. I searched but was unable to find a way to load actions at runtime.

var
  i : integer;
  AcList : TActionList;
begin
  // search for prewritten actionlist
  for i := 0 to Self.Owner.ComponentCount - 1 do
    if Components[i] is TActionList then
      AcList := TActionList(Components[i]);
  // if found then asscociate
  If Assigned(AcList) then
    MyButton.Action := AcList.Actions[0];
end;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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