Search This Blog

Lets go..


Dynamic MenuStrip and Access privilege in Windows Forms

By: Mohammad Tuaha


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.  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");  

            //string userid = "1001";
            //MnuStripItem = new ToolStripMenuItem(userid);

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


            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";
            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();


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

            // 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();

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




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