Solved

Loading actions at runtime

Posted on 2006-07-06
6
266 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now