Hello!

I have one main form (parent) which contains others child forms. The first child form is a login form, that after correct autentication opens another child form.

Depending on the user (operator or supervisor) some items of menustrip in form Parent should be available or not. The supervisor will have full menu available, the user only some items.

How can I do that? Any ideias?

Thanks a lot.
Tiago

Are you asking how to find particular menu items, and then enable/disable menu items? Or, is your question more general about how to design user permissions into the menu design?

Are you asking how to find particular menu items, and then enable/disable menu items? Or, is your question more general about how to design user permissions into the menu design?

It´s only to design user permissions into the menu.. my newbie idea is to pass user textbox value from login form and in other forms make a if cicle to check which user is and then disable or enable the menu items... it´s work? Can have some consequence? failure?

It´s only to design user permissions into the menu.. my newbie idea is to pass user textbox value from login form and in other forms make a if cicle to check which user is and then disable or enable the menu items... it´s work? Can have some consequence? failure?

The most simplest would be how you indicated you were thinking of doing it, but that limits your design to predefined usernames you evaluate in your if statements (at least as I understood you to say). If you do it like this, I would still define levels of access (levelUser and levelAdmin for example) and then set the menu items accordingly rather than have two or more separate code blocks that manipulate all the menu items in each block for each user...

A simple approach would be to create a table to store each user and menu identifiers for each menu option, as a boolean field switched on or off (true/false) to indicate whether the user has permission or not (menu item enabled or disabled).

Another simple, perhaps simpler, approach would be to create a table to store each user with a corresponding access level (some enumeration of values like: level0, level1, etc.), then use the level of access to determine whether the user has permission or not (menu item enabled or disabled), depending on what level of access is required to access that menu item.

As a more complex approach, you may wish to do both in that you have multiple tables defined: User, AccessLevel, MenuLevel, etc. In this approach, you would have:

  • a User table with each user and user's access level; also a Default user (guest?) with default access level perhaps...
  • an AccessLevel with access definitions they may or may not include the MenuLevel definitions...Say you want one user to have RW on some files, but only Read on others, but also an ADMIN that allows everything of course...
  • a MenuLevel table (each access level with menu item columns) with menu options on or off depending on the access level; you could also combine specific dialog buttons and stuff with additional columns, or add
  • a DialogLevel Table that defines specific access to dialog controls/functions...

In case you are wondering how to enable/disable the menu items, here are some code extractions I pulled from an old project I worked on that shows how to enable/disable menu items:

// DISABLE all menu items...
                // navigate through each parent
                foreach (ToolStripMenuItem mainMenu in menuStrip1.Items)
                {
                    // help always enabled...
                    if (mainMenu.Text == @"&Help")
                        continue;
             
                    // navigate through each submenu
                    foreach (ToolStripItem subMenu in mainMenu.DropDownItems)
                    {
                        subMenu.Enabled = false;
                    }
                }

                // Set individual menu items (ToolStripMenuItem)...
                /*
                mnuFileOpen.Enabled = true;
                mnuFileNew.Enabled = true;
                mnuFileSave.Enabled = dataMgr.DataChanged;
                
                mnuEditCopy.Enabled = dtvContextMenu.miCopy.Enabled;
                mnuEditCut.Enabled = dtvContextMenu.miCut.Enabled;
                mnuEditPaste.Enabled = dtvContextMenu.miPaste.Enabled;
                mnuEditDelete.Enabled = dtvContextMenu.miDelete.Enabled;
                */

I commented out the code where I set individual menu items so it would compile because I don't have those defined in the project I use to work with the forum. In this section of code, in your case, you decide to turn on or off menu items depending on whether the user has access. I only pulled a few of them for brevity, but it should get the point across I think.

Hope this helps! Cheers!

Thanks a lot DdoubleD! Brilliant post!
I will try to implement your sugestion!

Best regards!

The most simplest would be how you indicated you were thinking of doing it, but that limits your design to predefined usernames you evaluate in your if statements (at least as I understood you to say). If you do it like this, I would still define levels of access (levelUser and levelAdmin for example) and then set the menu items accordingly rather than have two or more separate code blocks that manipulate all the menu items in each block for each user...

A simple approach would be to create a table to store each user and menu identifiers for each menu option, as a boolean field switched on or off (true/false) to indicate whether the user has permission or not (menu item enabled or disabled).

Another simple, perhaps simpler, approach would be to create a table to store each user with a corresponding access level (some enumeration of values like: level0, level1, etc.), then use the level of access to determine whether the user has permission or not (menu item enabled or disabled), depending on what level of access is required to access that menu item.

As a more complex approach, you may wish to do both in that you have multiple tables defined: User, AccessLevel, MenuLevel, etc. In this approach, you would have:

  • a User table with each user and user's access level; also a Default user (guest?) with default access level perhaps...
  • an AccessLevel with access definitions they may or may not include the MenuLevel definitions...Say you want one user to have RW on some files, but only Read on others, but also an ADMIN that allows everything of course...
  • a MenuLevel table (each access level with menu item columns) with menu options on or off depending on the access level; you could also combine specific dialog buttons and stuff with additional columns, or add
  • a DialogLevel Table that defines specific access to dialog controls/functions...

In case you are wondering how to enable/disable the menu items, here are some code extractions I pulled from an old project I worked on that shows how to enable/disable menu items:

// DISABLE all menu items...
                // navigate through each parent
                foreach (ToolStripMenuItem mainMenu in menuStrip1.Items)
                {
                    // help always enabled...
                    if (mainMenu.Text == @"&Help")
                        continue;
             
                    // navigate through each submenu
                    foreach (ToolStripItem subMenu in mainMenu.DropDownItems)
                    {
                        subMenu.Enabled = false;
                    }
                }

                // Set individual menu items (ToolStripMenuItem)...
                /*
                mnuFileOpen.Enabled = true;
                mnuFileNew.Enabled = true;
                mnuFileSave.Enabled = dataMgr.DataChanged;
                
                mnuEditCopy.Enabled = dtvContextMenu.miCopy.Enabled;
                mnuEditCut.Enabled = dtvContextMenu.miCut.Enabled;
                mnuEditPaste.Enabled = dtvContextMenu.miPaste.Enabled;
                mnuEditDelete.Enabled = dtvContextMenu.miDelete.Enabled;
                */

I commented out the code where I set individual menu items so it would compile because I don't have those defined in the project I use to work with the forum. In this section of code, in your case, you decide to turn on or off menu items depending on whether the user has access. I only pulled a few of them for brevity, but it should get the point across I think.

Hope this helps! Cheers!

This question has already been answered. Start a new discussion instead.