disable menu items via code ? lazarus / 21-General ---------------------------------------------------------------------- rcmz · 2021-03-11 19:45 Hi to all, how can i disable menu items via code ? I have users with different menu enable items, I would like to disable the ones that they heve no permision to enter in that option. TIA. Ramiro ---------------------------------------------------------------------- Soner · 2021-03-11 20:24 MenuItemXYZ.Enabled:=false; ---------------------------------------------------------------------- MarkMLl · 2021-03-11 21:35 Quote from: Soner on March 11, 2021, 09:24:08 pmMenuItemXYZ.Enabled:=false; Plus also the .Visible property, depending on whether the requirement is "disable" or "hide". MarkMLl ---------------------------------------------------------------------- Sieben · 2021-03-11 21:55 'Disabled' is telling the inferiors what they're missing... ---------------------------------------------------------------------- MarkMLl · 2021-03-11 22:07 Quote from: Sieben on March 11, 2021, 10:55:46 pm'Disabled' is telling the inferiors what they're missing... It's equivalent to "This page intentionally blank", which is not something you will see in documents where security is a significant issue. Scenario: you might want to hide a menu entry if it pertained to an intentionally-omitted plugin if you didn't want user who'd not paid for that particular plugin to go hunting for it. Or you might want to hide diagnostic facilities since you didn't want users who thought they needed them to hassle your support staff for the unlock sequence. MarkMLl ---------------------------------------------------------------------- rcmz · 2021-03-11 23:40 Thanks for your fast response, I think I did not explain myself correctly, let me give you more info: I have at least 4 users each of them have different permisions to the options of the menu I need that when the user signs in, disable or hide the options of the menu or menu items according to there permisions. TIA Ramiro. ---------------------------------------------------------------------- MarkMLl · 2021-03-12 08:08 Quote from: rcmz on March 12, 2021, 12:40:06 amI have at least 4 users each of them have different permisions to the options of the menu I need that when the user signs in, disable or hide the options of the menu or menu items according to there permisions. I fail to see the problem. Keep a record of which items are to be disabled/hidden per user. MarkMLl ---------------------------------------------------------------------- balazsszekely · 2021-03-12 08:55 QuoteI think I did not explain myself correctly, let me give you more info: I have at least 4 users each of them have different permisions to the options of the menu I need that when the user signs in, disable or hide the options of the menu or menu items according to there permisions. You did explain it correctly. You should do something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if FUserName = 'admin' then  begin    MenuItem1.Enabled := True;    //..  end  else if FUserName = 'regularuser' then  begin    MenuItem1.Enabled := False;  end; or even better: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   case FUserRights of    0: begin         MenuItem1.Enabled := False; //user with no rights         MenuItem1.Visible := False;       end;    1: begin         MenuItem1.Enabled := False; //user with read-only rights         MenuItem1.Visible := True;        end;    2: begin       MenuItem1.Enabled := True; //user with full rights         MenuItem1.Visible := True;       end;  end; ---------------------------------------------------------------------- rcmz · 2021-03-15 18:42 Hi, how can I handle all the menuitems considering that there are items in MenuItem1, MenuItem2 and MenuItem3 ? TIA Ramiro ---------------------------------------------------------------------- howardpc · 2021-03-15 20:00 A very simple example is attached. ---------------------------------------------------------------------- rcmz · 2021-03-15 22:34 hi, thanks for the sample, I tried to load the project but I dont get any file in the object inspector, tried to run it but nothing happens. TIA Ramiro ---------------------------------------------------------------------- Josh · 2021-03-15 22:49 Hi If your not using the tag property for anything you could use this to determine if it should be enabled ie a tag of 0 means control is available to any access rights, value 1 acess to user rights level 1 and above value 2 acess to user rights level 2 and above. You can then iterate though all components and set items accordingly, below is just for mainmenu and menuitems, but easily can add any other component control. User_Rights is a current User Rights Make_UnAvailable_Items_Visible is true or false depending if you want non enabled items visible. Note not tested or debugged. You would call the routine whenever user rights have changed. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Var User_Rights:LongInt=99; // default to adminMake_UnAvailable_Items_Visi ble:Boolean=True;  ............  Procedure TForm1.UpdateMenuItemsBasedOnUSerRights;Var I:LongInt;begin  with TForm(Self) do  begin    for i := 0 to ComponentCount - 1 do    Begin      If Components[i] is TMainMenu Then      Begin        with Components[i] as TMainMenu DO        begin          enabled:=Tag<=User_Rights;          If Make_UnAvailable_Items_Visible Then visible:=True          else visible:=Enabled;        end;      end;      If Components[i] is TMenuItem then      Begin        with Components[i] as TMenuItem DO        begin          enabled:=Tag<=User_Rights;          If Make_UnAvailable_Items_Visible Then visible:=True else          visible:=Enabled;        end;      end;    end;  end;end; ---------------------------------------------------------------------- howardpc · 2021-03-16 07:17 Quote from: rcmz on March 15, 2021, 11:34:18 pmthanks for the sample, I tried to load the project but I dont get any file in the object inspector, tried to run it but nothing happens. Sorry, I forgot to set the "compatibility" checkbox in the project options, so if you are not running a trunk or recent Lazarus your Lazarus cannot read the files. Corrected in the new attachment.