Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Toggling and enabling of menustrip items and sub items.

Toggling and enabling of menustrip items and sub items.

Scheduled Pinned Locked Moved C#
csharphelpquestion
11 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sunitkrishna
    wrote on last edited by
    #1

    I have a doubt in the enabling and disabling of menustrip items and subitems (C#.Net). This is to be done based on user permission settings for every level and sublevel of menus. I would like to use a recursive function to make this happen. I am setting the userlevel(menu id) as the tag of each menustripitem. Can anyone help me to achieve this? This is very urgent. regards, sunitha

    SRK

    L 1 Reply Last reply
    0
    • S sunitkrishna

      I have a doubt in the enabling and disabling of menustrip items and subitems (C#.Net). This is to be done based on user permission settings for every level and sublevel of menus. I would like to use a recursive function to make this happen. I am setting the userlevel(menu id) as the tag of each menustripitem. Can anyone help me to achieve this? This is very urgent. regards, sunitha

      SRK

      L Offline
      L Offline
      Lev Danielyan
      wrote on last edited by
      #2

      Here is how you can traverse through the menu items recursively: foreach (ToolStripMenuItem item in menuStrip1.Items) { process(item); } private void process(ToolStripMenuItem item) { MessageBox.Show(item.Text); if (item.DropDownItems.Count == 0) return; foreach (ToolStripMenuItem subitem in item.DropDownItems) { process(subitem); } }

      Regards, Lev

      S 1 Reply Last reply
      0
      • L Lev Danielyan

        Here is how you can traverse through the menu items recursively: foreach (ToolStripMenuItem item in menuStrip1.Items) { process(item); } private void process(ToolStripMenuItem item) { MessageBox.Show(item.Text); if (item.DropDownItems.Count == 0) return; foreach (ToolStripMenuItem subitem in item.DropDownItems) { process(subitem); } }

        Regards, Lev

        S Offline
        S Offline
        sunitkrishna
        wrote on last edited by
        #3

        Thanks Lev for your help. But my problem is that i am using a datatable to find the user levels of the logged on user.the data retrieved from the datatble has values like :- 8(main menu item) 8.01(sub menu) 8.02 8.03 8.04 8.04.01(sub item of 8.04) 8.04.02 8.05 8.06 8.06.01(sub item of 8.06) 8.06.02 8.06.02.01(sub item of 8.06.02) 8.06.02.02 these values are the menu ids that i set in the user permission screen. I want to iterate thru the datatable as well.getting a bit confused with all the looping. Please help me out........... thanks in advance

        SRK

        L 1 Reply Last reply
        0
        • S sunitkrishna

          Thanks Lev for your help. But my problem is that i am using a datatable to find the user levels of the logged on user.the data retrieved from the datatble has values like :- 8(main menu item) 8.01(sub menu) 8.02 8.03 8.04 8.04.01(sub item of 8.04) 8.04.02 8.05 8.06 8.06.01(sub item of 8.06) 8.06.02 8.06.02.01(sub item of 8.06.02) 8.06.02.02 these values are the menu ids that i set in the user permission screen. I want to iterate thru the datatable as well.getting a bit confused with all the looping. Please help me out........... thanks in advance

          SRK

          L Offline
          L Offline
          Lev Danielyan
          wrote on last edited by
          #4

          You can associate a user level (say an integer, as a Tag) with menu item, then get the current user privilege level and finally iterate through the menu (with the code I posted) and disable those items whose privilege level is higher than the current one. This way you won't have any looping problems, just go through the menus recursively :)

          Regards, Lev

          S 1 Reply Last reply
          0
          • L Lev Danielyan

            You can associate a user level (say an integer, as a Tag) with menu item, then get the current user privilege level and finally iterate through the menu (with the code I posted) and disable those items whose privilege level is higher than the current one. This way you won't have any looping problems, just go through the menus recursively :)

            Regards, Lev

            S Offline
            S Offline
            sunitkrishna
            wrote on last edited by
            #5

            Lev, this is the current code i am using:- private void SetUserPermissions(int userId,int grpId) { DataTable objDt = objUserlBus.GetAllUserLevel(userId); for (int i = 0; i < objDt.Rows.Count; i++) { foreach (ToolStripMenuItem menu in objMain.menuStrip1.Items) { if (objDt.Rows[i][1].ToString() == menu.Tag.ToString()) { menu.Enabled = true; } EnableMenu(menu, objDt, 0); } } } private void EnableMenu(ToolStripMenuItem tm, DataTable dt, int strt) { try { foreach (ToolStripMenuItem item in tm.DropDownItems) { for (int i = strt; i < dt.Rows.Count; i++) { if (_usrGrp == 2 && item.Tag.ToString() == "7.01") continue; if (item.Tag.ToString() == dt.Rows[i][1].ToString()) { item.Enabled = true; EnableMenu(item, dt, i + 1); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } Please go thru this n verify whether i have gone wrong anywhere. regards,

            SRK

            L 1 Reply Last reply
            0
            • S sunitkrishna

              Lev, this is the current code i am using:- private void SetUserPermissions(int userId,int grpId) { DataTable objDt = objUserlBus.GetAllUserLevel(userId); for (int i = 0; i < objDt.Rows.Count; i++) { foreach (ToolStripMenuItem menu in objMain.menuStrip1.Items) { if (objDt.Rows[i][1].ToString() == menu.Tag.ToString()) { menu.Enabled = true; } EnableMenu(menu, objDt, 0); } } } private void EnableMenu(ToolStripMenuItem tm, DataTable dt, int strt) { try { foreach (ToolStripMenuItem item in tm.DropDownItems) { for (int i = strt; i < dt.Rows.Count; i++) { if (_usrGrp == 2 && item.Tag.ToString() == "7.01") continue; if (item.Tag.ToString() == dt.Rows[i][1].ToString()) { item.Enabled = true; EnableMenu(item, dt, i + 1); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } Please go thru this n verify whether i have gone wrong anywhere. regards,

              SRK

              L Offline
              L Offline
              Lev Danielyan
              wrote on last edited by
              #6

              Why are you getting all the users permissions? There is only one user that works with the program instance. It is enough to get current user's permission level, and then iterate through the menus, tuning it as you need

              Regards, Lev

              S 1 Reply Last reply
              0
              • L Lev Danielyan

                Why are you getting all the users permissions? There is only one user that works with the program instance. It is enough to get current user's permission level, and then iterate through the menus, tuning it as you need

                Regards, Lev

                S Offline
                S Offline
                sunitkrishna
                wrote on last edited by
                #7

                I am getting only the currently logged in user's permissions into the datatable,by passing his userid.I think you got confused by then name of the function.

                SRK

                L 1 Reply Last reply
                0
                • S sunitkrishna

                  I am getting only the currently logged in user's permissions into the datatable,by passing his userid.I think you got confused by then name of the function.

                  SRK

                  L Offline
                  L Offline
                  Lev Danielyan
                  wrote on last edited by
                  #8

                  Well, I see, You are storing all the menu IDs applicable to the user permissions in a DB and then enable/disable the menu items with this IDs, am I right? I don't think this is efficient, you are storing a lot of redundant data, try to do it the way I suggested. Do not keep menu IDs in the DB, instead keep the user privilege level, associate it with each menu item. This way you will get just the user permissions level from db (an integer for example) and compare it with the level associated with the menu item. See my previous post.

                  Regards, Lev

                  S 1 Reply Last reply
                  0
                  • L Lev Danielyan

                    Well, I see, You are storing all the menu IDs applicable to the user permissions in a DB and then enable/disable the menu items with this IDs, am I right? I don't think this is efficient, you are storing a lot of redundant data, try to do it the way I suggested. Do not keep menu IDs in the DB, instead keep the user privilege level, associate it with each menu item. This way you will get just the user permissions level from db (an integer for example) and compare it with the level associated with the menu item. See my previous post.

                    Regards, Lev

                    S Offline
                    S Offline
                    sunitkrishna
                    wrote on last edited by
                    #9

                    I dont understand how the data will b eredundant,as the fields in the usrlvlmas table are usr_id n menu_id. Each time new permissions are set for the user,all the existing levels for that user are deleted and only new ones are updated. Anyway let me have a try again.

                    SRK

                    L 1 Reply Last reply
                    0
                    • S sunitkrishna

                      I dont understand how the data will b eredundant,as the fields in the usrlvlmas table are usr_id n menu_id. Each time new permissions are set for the user,all the existing levels for that user are deleted and only new ones are updated. Anyway let me have a try again.

                      SRK

                      L Offline
                      L Offline
                      Lev Danielyan
                      wrote on last edited by
                      #10

                      The key of my suggested approach is having general permission groups (levels, like admin, power user, restricted user) and not individual permissions for each and every user, that's generally the way it is being done (e.g. in Asp.Net membership providers).

                      Regards, Lev

                      S 1 Reply Last reply
                      0
                      • L Lev Danielyan

                        The key of my suggested approach is having general permission groups (levels, like admin, power user, restricted user) and not individual permissions for each and every user, that's generally the way it is being done (e.g. in Asp.Net membership providers).

                        Regards, Lev

                        S Offline
                        S Offline
                        sunitkrishna
                        wrote on last edited by
                        #11

                        Ok.Thanks...

                        SRK

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups