September 27th, 2013, 02:28 AM
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
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
But I don't want users even seeing what reports are available for Topic B without a password.
procedure TFrmMainForm.ReloadRawdataClick(Sender: TObject);
if AdminPasswordDlg.ModalResult = mrOK then
September 27th, 2013, 08:21 AM
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.
Then when the password is verified:
procedure TForm1.FormCreate(Sender: TObject);
Menu1.visible := False;
Menu1.Shortcut := TextToShortCut('');
Menu2.visible := False;
Menu2.Shortcut := TextToShortCut('');
SignInMenuItem.Visible := False;
SignInMenuItem.ShortCut := TextToShortCut('');
Menu1.visible := True;
Menu1.Shortcut := TextToShortCut('Alt+H');
Menu2.visible := True;
Menu2.Shortcut := TextToShortCut('Alt+I');
September 27th, 2013, 11:37 AM
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 !