Search This Blog

Lets go..

Monday

Dynamic MenuStrip and Access privilege in Windows Forms

By: Mohammad Tuaha


  • Creating Dynamic Menus and Submenus and access privilege in C#
  • Create MenuStrip Dynamically and User role in C#
    DownLoad Project


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


DownLoad Project






3.

 

4 comments:

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

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. DOWNLOAD FULL PROJECT https://drive.google.com/file/d/0B55z6hBNJXFEbklpWWlXOVJsbkk/view

    ReplyDelete

Thanks for your opinion