Search This Blog

Lets go..




Manage your Inventory | Grow your restaurant business | Manage your Employee | Control your accounts

Monday

Dynamic MenuStrip and Access privilege in Windows Forms

By: Mohammad Tuaha



Introduction

This article explain how to create a Database Driven MenuStrip and add Items to the MenuStrip using the ToolStripMenuItem class in a C# Windows Forms application.

First we need to create Database here I am using Sqlite DB. This more comfortable and platform dependent.  

2

2.  Parent Form


33. Manage User Form


3.1. Give User Privilege

 

----

     Code Section

    private void FrmParent_Load(object sender, EventArgs e)
        {
            // To make this Form the Parent Form

           this.IsMdiContainer = true;


            //Creating object of MenuStrip class

            MnuStrip = new MenuStrip();

            ////MnuStripItem = new ToolStripMenuItem("Exit");  
            ////MnuStrip.Items.Add(MnuStripItem);

            //string userid = "1001";
            //MnuStripItem = new ToolStripMenuItem(userid);
            //MnuStrip.Items.Add(MnuStripItem);

            //////Placing the control to the Form

            this.Controls.Add(MnuStrip);


            String connectionString ;
            connectionString = @"Data Source=dynamMenu.db;Version=3;New=False;Compress=True";
           // connectionString = "Data Source=(local); Initial Catalog=apostest; User ID=User_Profile;Password=User_Profile";
            //ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
            conn = new SQLiteConnection(connectionString);
            String Sequel = "SELECT MAINMNU,MENUPARVAL,STATUS FROM MNU_PARENT where status = 'Y'";
            SQLiteDataAdapter da = new SQLiteDataAdapter(Sequel, conn);
            DataTable dt = new DataTable();

            conn.Open();
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                MnuStripItem = new ToolStripMenuItem(dr["MAINMNU"].ToString());
                SubMenu(MnuStripItem, dr["MENUPARVAL"].ToString());
                MnuStrip.Items.Add(MnuStripItem);
            }

            // The Form.MainMenuStrip property determines the merge target.
            this.MainMenuStrip = MnuStrip;
           
        }
       

        public void SubMenu(ToolStripMenuItem mnu, string submenu)
        {
          // String Seqchild = "SELECT FRM_NAME FROM MNU_SUBMENU WHERE MENUPARVAL='" + submenu + "'";
            String Seqchild = "SELECT FRM_NAME FROM vw_pagerole WHERE MENUPARVAL='" + submenu + "' and status = 1 and UID = '" + toolUserID.Text + "' ";
            SQLiteDataAdapter dachildmnu = new SQLiteDataAdapter(Seqchild, conn);
            DataTable dtchild = new DataTable();
            dachildmnu.Fill(dtchild);

            foreach (DataRow dr in dtchild.Rows)
            {
                ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr["FRM_NAME"].ToString(), null, new EventHandler(ChildClick));
                mnu.DropDownItems.Add(SSMenu);
            }

        }


---

Summary

In this article we have discussed how to create a Database Driven MenuStrip and access privilege in a Windows Form and adding Submenus using the ToolStripMenuItem class and registering the Event for the Child Forms and navigating through the forms in the Project to associate them with the Form Name dynamically in the database.

After finishing this article you can learn
How to create Login using Database.
How to create dynamic Menu
User access privilege / User role assign


Load Full Source Code






3.

 

4 comments:

  1. Very good coding. it's help me a lot.

    ReplyDelete
  2. How to Open Form With dynamic Menu buttons

    ReplyDelete
  3. you explain very well. you sale my time.
    https://bit.ly/320NS8j

    ReplyDelete

Thanks for your opinion