How to dynamically control mdi menu c#.Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ppaul4
    New Member
    • Oct 2009
    • 4

    How to dynamically control mdi menu c#.Net

    Hi Dear All,

    I create a window application in c#.net. I want to hide menu options when particular user login for example: in mdi menu there is Report menu there is report1 and report2 now when user1 login I want to show all option report1 and report2 but when user2 login I want to show only report1.

    In vb6 it is very essay but because I am new in c#.net I couldn’t find the solution.
    Please help me to find the solution.

    Thanks and Regards
    Prakash
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You should just be able to set the tool strip item's visible property to false. I just tested it and it appears to work.

    Comment

    • ppaul4
      New Member
      • Oct 2009
      • 4

      #3
      fine here is the code

      foreach (ToolStripMenuI tem tsmi in menuStrip.Items )
      {
      if (tsmi.Text == "Security")
      {
      /// some code
      }
      }

      in mdi load i call user login form, now as per user permission i want to hide/unhide the menu options

      problem
      1. this code is work f9 when i put this in mdi load but in user form it will return null object reference.
      2. it only return the mater menu like only file option not files child node

      please help or provide some code

      problem

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I don't understand what the problem is... is it that you can't hide/show menu selections, or that you're getting a null reference exception when you run?

        Comment

        • ppaul4
          New Member
          • Oct 2009
          • 4

          #5
          I am getting null reference

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Where are you hitting the null reference? If you're using Visual Studio, you should be able to run it in debug mode (F5) and it will break where the exception is thrown.

            From the code you posted, the only place I can see a null reference occurring is menuStrip, but you've given no context so I can't tell why it would be null.

            You mentioned that it doesn't work when you put it in the user form... I don't know what that is with respect to your code, but make sure it knows what menuStrip is, or has it initialized.

            Comment

            • ppaul4
              New Member
              • Oct 2009
              • 4

              #7
              /// Application start from here
              static void Main()
              {
              Application.Ena bleVisualStyles ();
              Application.Set CompatibleTextR enderingDefault (false);
              Application.Run (new MDIColts ());
              }

              /// MDI load code having no problem
              /// menuStrip is private System.Windows. Forms.MenuStrip menuStrip;

              private void MDIColts_Load(o bject sender, EventArgs e)
              {
              foreach (ToolStripMenuI tem tsmi in menuStrip.Items )
              {
              if (tsmi.Text == "Security" || tsmi.Text == "Master" || tsmi.Text == "Transactio ns" || tsmi.Text == "Reports")
              {
              getChildNodes(t smi);

              }
              }

              MasterUI.frmLog in.GetInstance. ShowDialog();
              }

              ///the time of MDI load I call MasterUI.frmLog in.GetInstance. ShowDialog();
              /// and in the click of user login
              private void btLogin_Click(o bject sender, EventArgs e)
              {
              try
              {
              if (UserLogIn.bAct ive == false)
              {
              MessageBox.Show (MessageResourc e.NotActive, "Informatio n", MessageBoxButto ns.OK, MessageBoxIcon. Information);
              txtUserId.Text = "";
              txtPassword.Tex t = "";
              txtUserId.Focus ();
              }
              else
              {
              /// code where collecting data from database , security table and as per permissions want to show mdi menu items
              ///menuStrip.Items null ref problem
              foreach (ToolStripMenuI tem tsmi in menuStrip.Items )
              {
              if (tsmi.Text == "Security" || tsmi.Text == "Master" || tsmi.Text == "Transactio ns" || tsmi.Text == "Reports")
              {

              }
              }


              if you have any good suggestion or help please provide me

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                In the future, can you please use code tags when you post? It makes it a lot easier to read. You can find some information here: http://bytes.com/misc.php?do=bbcode

                Anyway, so the btLogin_Click method is in the user form, right? It looks like you're trying to access the main form's menuStrip from the child form... you shouldn't even be able to see that because it's private.

                I realize this might be something to do with MDI (which I don't have any experience with), but I'll still try to help you out if I can.

                Basically, how are you accessing menuStrip from the user form when it actually belongs to the main form? Is there some way to access the user form's parent so you can get a hold of the parent's menu strip?

                That said, a better approach might be to have the user form return a login status and let the parent form show or hide it's own menu items. It would better avoid confusion like this.

                Comment

                Working...