简体   繁体   中英

Disable and Enable ToolStripMenuItem Programmatically

I'm setting up a User and Admin Permissions which works perfectly well but I want to disable some ToolStripMenuItem from "Users" but Enable them only for "Admin".

Here's the code that logs User and Admin in depending on their Roles.

if (dt.Rows[0][0].ToString() == "1")
{
  if (ComboBox_LoginAs.SelectedIndex == 0)
{
  this.Hide();                     
  Main_Form mainSystem = new Main_Form();
  mainSystem.Show();
}
else if (ComboBox_LoginAs.SelectedIndex == 1)
{
  this.Hide();
  Main_Form mainSystem = new Main_Form();
  mainSystem.Show();

  /* disable View All Employees for Users */

  ViewAllEmployeesToolStripMenuItem.Enable = false;
}

I expect the Users to not be able to view all Employee Records except the Admin. I hope this question will not get down voted.

Thank you for your help!!

Here is an example of an interface I used once that accomplishes something similar.

Main

namespace UnNamed Project
{
    class Program
    {
        static void Main(string[] args)
        {
            User user;
            user = new SuperUser("Bob", "12345");

            if (user.Login())
            {
                Console.WriteLine($"Welcome {user.Name}");
            }
            else
            {
                Console.WriteLine("An error has occured.");
            }

            Utility.PauseBeforeContinuing();
        }
    }
}

Interface Class

namespace UnNamed Project
{
    interface ILoginHandler
    {
        bool HandleLogin();
        void SetPassword(string password);
    }
}

User Class

namespace UnNamed Project
{
    abstract class User
    {
        private string _name;
        private int _securityLevel;

        public User(string name, int securityLevel)
        {
            _name = name;
            _securityLevel = securityLevel;
        }

        abstract public bool Login();
    }
}

SuperUser Class

namespace UnNamed Project
{
    class SuperUser : User
    {
        private ILoginHandler _loginHandler;

        public SuperUser(string name, string password) : base(name, 10)
        {
            _loginHandler = new FaceLogin(password);
        }

        public override bool Login()
        {
            return _loginHandler.HandleLogin();
        }
    }
}

As you can see, if setup properly you can create an object "User" and another object "Admin" and then create an abstract method within user. You can alternatively do a virtual method to handle the login, just make sure that your admin class has an override for it's own Login() method. This will cause the program to use the method directly related to the object.

Okay, so I tried changing my MenuStripItem Modifier state to "public". Then I created an object on my main form.

public static Main_Form GetMainForm_Obj;
public Main_Form()
    {
        InitializeComponent();
    }

Then inside my Form Load, I added:

private void Main_Form_Load(object sender, EventArgs e)
    {
        eMPLOYEESToolStripMenuItem.Enabled = true;

        GetMainForm_Obj = this;
    }

And finally, in my Login form:

 if (ComboBox_LoginAs.SelectedIndex == 0)
  {
     this.Hide();
     Main_Form mainSystem = new Main_Form();
     mainSystem.Show();
  }
  else if (ComboBox_LoginAs.SelectedIndex == 1)
  {
     this.Hide();
     Main_Form mainSystem = new Main_Form();
     mainSystem.Show();

     Main_Form.GetMainForm_Obj.eMPLOYEESToolStripMenuItem.Enabled = false;
   }

Thank you everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM