Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to add password security to a specific menu in my WPF application.

I've made an InputBox appear when the menu is clicked, but when the password is entered, the menu is still closed.

I feel like I'm missing a method to programmatically invoke the menu, or some command in the menu click event to continue the opening.

The InputBox is just a crude, first stab. I've read about the PasswordBox control, that sounds a bit more purpose built, but I am just trying to understand the MenuItem event firing at this stage.

Any suggestions welcome.

What I have tried:

VB
Private Sub mData_SubmenuOpened(sender As Object, e As RoutedEventArgs) Handles mData.SubmenuOpened
    If InputBox("Enter password:", "Data Menu Password") <> "***" Then
        mDataPickup.IsEnabled = False
        mArchive.IsEnabled = False
    Else
        mDataPickup.IsEnabled = True
        mArchive.IsEnabled = True
    End If
End Sub
Posted
Updated 12-Dec-21 12:08pm

IMHO, you should have the user log in to the app, and then configure/establish program features based on that user's role(s). That way, you don't need custom menu/event handling, because everything presented to the user is available to that user.

EDIT ===================

I suppose you could use commands on specific menu items and buttons, and set the CanExecute property based on whether or not the user has clicked an item within the alloted time. That's gonna add a crapload of code (both markup and code-behind). You can do it without commands, and reduce the amount of markup you need, but that would require even more code-behind, and be harder to maintain.

Another way to do it is to fire up a thread that monitors system mouse clicks, and if it doesn't detect a click within the timeout, it throws up a form that forces the user to log in again.
 
Share this answer
 
v2
Comments
Donny Hardo 9-Dec-21 18:02pm    
I agree in principle. However the workstations are in a shared environment and the users just want an extra added layer of security/confirmation to perform the actions.
#realJSOP 10-Dec-21 5:17am    
I give them about two weeks before they tell you to undo it because it's interrupting their workflow. :)

How about this: After five minutes without having clicked anything, it automatically blacks out the window and prompts the user to re-enter their password.
#realJSOP 12-Dec-21 7:42am    
I edited my answer.
I found this article:

Show menu programmatically in WPF - Stack Overflow[^]

And have modified my code a bit.. It is still not working as I'd like. I need to sort out the order of event firing, but this might work

Private Sub mData_Click(sender As Object, e As RoutedEventArgs) Handles mData.Click
    e.Handled = True
    If InputBox("Password:", "Password") = "**" Then
        Dim dataMenu As System.Windows.Controls.MenuItem = mData
        dataMenu.IsSubmenuOpen = True
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900