#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0

    Putting a password on a TmenuItem submenu?


    I have a TMainMenu that has several TmenuItems, each of which has their own submenu to form a menu tree that gets expanded as the user clicks each one eg
    Code:
    
    Reports
        Topic A
             Report A1
             Report A2
        Topic B
             Report B1
             Report B2
        Topic C
             Report C1
             Report C2
    
    Reports under Topic B are only for management.

    How can I make it so that, if the user clicks 'Topic B' then they get asked for a password before the submenu items 'Report B1' and 'Report B2' get expanded and shown?

    (I already have code on some TmenuItems that prevent the process from happening without a password ie
    Code:
    procedure TFrmMainForm.ReloadRawdataClick(Sender: TObject);
    begin
    AdminPasswordDlg.showmodal;
    if AdminPasswordDlg.ModalResult = mrOK then
            FrmInport.ImportDataStart;
    end;
    But I don't want users even seeing what reports are available for Topic B without a password.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    I'd say the proper way to accomplish this is to have your users sign in with a username/password when the application starts. Depending on the username, your application can determine which options to make available.

    If that isn't an option then another approach is to set all the "restricted" menu item's visible property to false. Add one more menu item to the restricted Menu item such as "Sign in as Manager" that will prompt the user for the password. If correct, then set visible to all your "restricted" menu items to true, and set the visible property to your "sign in as manager" menu item to false.

    One more thing, making your menu item invisible does not disable any shortcut associated with the menu item, and I think Delphi will automatically assign short cuts to a menu item by default when your application starts. So in your OnCreate method, put the code to set the menu items and clear any short cut.
    Code:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
       Menu1.visible := False;
       Menu1.Shortcut := TextToShortCut('');
       Menu2.visible := False;
       Menu2.Shortcut := TextToShortCut('');
       ...
    end;
    Then when the password is verified:
    Code:
    procedure TForm1.PasswordVerified;
    begin
       SignInMenuItem.Visible := False;
       SignInMenuItem.ShortCut := TextToShortCut('');
       Menu1.visible := True;
       Menu1.Shortcut := TextToShortCut('Alt+H');
       Menu2.visible := True;
       Menu2.Shortcut := TextToShortCut('Alt+I');
       ...
    end;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    18
    Rep Power
    0
    Thanks, I hadn't thought of the shortcut.
    I guessed I might have to do it using the visible property.

    I don't really want to make every user log in as it is used by around 100 users, only a handful of whom I want to use those particular menus (as well as all the other menus).

    Having login code and password verification for everyone is unnecessary and too much hassel - for them as well as me !

IMN logo majestic logo threadwatch logo seochat tools logo