Set Enabled ActionList Property to True

0

I store in a table the name of my Actions, and dynamically through a query, I return them on screen, as follows:

I declare a variable of the type:

MinhaACL : TAction;

Begin
   MinhaACL := TAction(qryActions.FieldByName('nomeacl').Asstring);
   MinhaACL.Enabled := True;
End;

But when I try to Enable it with Enable, the system shows the following error:

  

Access violation at address 3B90C301 in module 'BetterFuell.exe'. Read   of address 3B90C301

Is there any other way for me to do this? If anyone can give me a Light.

And I tried to put it like this

MinhaACL := TAction('NomeDaMinhaACL'); 
MinhaACL.Enabled := True;
    
asked by anonymous 20.02.2018 / 16:27

2 answers

1

The problem is that you have the action name but can not convert it directly to an object of type TAction. Assuming that your Actions are within a TActionList (correct me if you're mistaken), you can do a function to get the name of a TAction, loop the TActionList, and return the TAction that has the desired name. Something like

function FindActionByName(SearchName:String;ActionList:TActionList):TAction;
var
  at:TAction;
  a: Integer;
begin
  result:=nil;
  for a := 0 to ActionList.ActionCount-1 do
    if ActionList[a].Name=SearchName then
    begin
      result:= TAction(ActionList[a]);
      break;
    end;
end;
    
20.02.2018 / 19:35
0

Complete code as received help:

procedure TMainForm.ValidaFormsLiberados;
var
 // Declarei as variáveis necessárias
  ArrQtdReg : Array of string;
  Registros, i, a: Integer;
  ChaveACL : string;
begin
  // Buscando Registros na minha query
  with qryPermitidos do
    begin
      Close;
      Open;
      Last;
      First;
    end;

  with qryFormsLocate do
    begin
      Close;
      Open;
    end;

    // Contando Registros
    Registros := qryPermitidos.RecordCount;
    SetLength(ArrQtdReg, Registros);

    // Desativando todas as minhas Actions
    for a := 0 to aclMenuPrincipal.ActionCount-1 do
      begin
        TCustomAction(aclMenuPrincipal.Actions[a]).Visible := False;
      end;


    // Liberando somente as Actions cadastradas na minha Query

    for I := 1 to Registros do
      begin
          ChaveACL          := qryPermitidosform_actionlist.AsString;
          ArrQtdReg[I]      := 'String '+ChaveACL;
          qryPermitidos.Next;

          for a := 0 to aclMenuPrincipal.ActionCount-1 do
            if aclMenuPrincipal[a].Name=ChaveACL then
            begin
              TCustomAction(aclMenuPrincipal.Actions[a]).Visible := True;
              break;
            end;
      end;

end;

Then I just called my procedure when creating my main form.

Thanks to Tiago Rodrigues for the help.

    
20.02.2018 / 20:40