Set Actionlist Enabled Property to True

Asked

Viewed 206 times

1

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

declare a variable of type:

MinhaACL : TAction;

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

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

Access Violation at address 3B90C301 in module 'Betterfuell.exe'. Read of address 3B90C301

Is there any other way I can do that? If someone can give me a Light.

OBS: And I’ve tried to put it like this

MinhaACL := TAction('NomeDaMinhaACL'); 
MinhaACL.Enabled := True;
  • No, I’ve already tested, she’s returning the data. Dei um Showmessage(qryActions.Fieldbyname('nomecl').Asstring) and returns the data correctly. E already tried to put it like this Minhaacl := Taction('Name daminhaacl'); Minhaacl.Enabled := True; E returns with the same error

  • You’re trying to convert a string to a Taction object... of course it’s a mistake. You need to use the string to search for the action, maybe for the components of your form, for example.

  • @Tiago-Rodrigues, you would have an example of how to locate an action through a string ?

2 answers

2

The problem is that Voce has the name of the action but cannot directly convert it to a Taction type Object. Assuming your Actions are inside a Tactionlist (correct me if I’m wrong), you can make a function to receive 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;
  • Thanks, gave it right here @Tiago-Rodrigues.

1

Complete code as help received:

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 only called my prior when creating my main form.

Thanks to Tiago Rodrigues for his help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.