假设有多个action对象,名字分别为:ac001,ac002,ac003...ac[n],tag值分别为1,2,3,n .现在有一菜单menu,通过变量i,指定menu的不同的ACTION,即当i为1的时候,menu.action:=ac001,当i=2时,menu.action:=ac002,i=n时,menu.action:=ac[n].
这句话该怎么写?通过名字怎么写?通过tag又怎么写?
注:不要用判断语句if i=1 then menu.action :=ac001等。

解决方案 »

  1.   

    前提:多个action对象都属于ActionList1function  TForm1.GetAction(sName: String): TContainedAction;
    var
      iLoop : Integer;
    begin
      Result := nil;
      for iLoop := 0 to ActionList1.ActionCount - 1 do
      begin
        if ActionList1.Actions[iLoop].Name = sName then
        begin
          Result := ActionList1.Actions[iLoop];
          break;
        end;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Menu.Action := GetAction('001');
    end;
      

  2.   

    use FindComponent to have a try
    for example
    var
      sName;
    sName := '000'+IntToStr(i);
    sName := 'ac'+SubString(sName, Length(sName)-2, 3);
    if (FindComponent(sName) != nil and FindComponent(sName).InheritsFrom(TACTION)) then
      menu.action := TACTION(FindComponent(sName));
      

  3.   

    TEdit(findcomponent('Edit'+inttostr(i))).text:=TAction(findwomponent('action'+inttostr(i))).........