blog

Home / DeveloperSection / Blogs / MenuStrip with Nested Submenus

MenuStrip with Nested Submenus

Shankar M5683 17-Feb-2013

MenuStrip with Nested Submenus

 

In my previous article I have discussed how to create a Database Driven Menu http://www.mindstick.com/Articles/26dc0999-6809-471b-93e4-74c7762d37a9/?Dynamic%20MenuStrip%20in%20Windows%20Forms  in Windows Forms. In this blog, I have discussed how to create Nested Submenus, where I have altered the Table Definition a little.

Table Definitions:

Parent Menu:

CREATETABLE [dbo].[MNUPARENT](

                [MENUPARVAL] [int] IDENTITY(1,1)NOTNULLPRIMARYKEY,

                [MENUNAME] [varchar](20)NOTNULL,

                [STATUS] [varchar](1));

Sample Insert Statements:

INSERTINTO MNUPARENT(MENUNAME,STATUS)

VALUES('Offline','Y');

 

INSERTINTO MNUPARENT(MENUNAME,STATUS)

VALUES('Main Line','Y');

 

INSERTINTO MNUPARENT(MENUNAME,STATUS)

VALUES('Administration','Y');

 

Child Menu:

CREATETABLE [dbo].[MNUSUBMENU](

                [MENUPARVAL] [int] NOTNULL,

                [FRM_CODE] [varchar](50),

                [FRM_NAME] [varchar](20)NOTNULL,

                [MNUSUBMENU] [int] NOTNULLPRIMARYKEY,

                [STATUS] [varchar](1))

Sample Insert Statements:

 

INSERTINTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(1,'FrmFinance','Finance',10,'Y')

 

INSERTINTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(1,NULL,'Accounting',20,'Y')

 

INSERTINTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(20,NULL,'Audit',30,'Y')

 

INSERTINTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(30,'FrmAccount','Acc. Standards',40,'Y')

Updated Code Version

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.Configuration;

using System.Reflection;

 

namespace DynamicMenuStripDBDriven

{

    publicpartialclassFrmParent : Form

    {

        SqlConnection conn;

        MenuStrip MnuStrip;

        ToolStripMenuItem MnuStripItem;

      

        public FrmParent()

        {

            InitializeComponent();

        }

 

        privatevoid FrmParent_Load(object sender, EventArgs e)

        {

            // To make this Form the Parent Form

            this.IsMdiContainer = true;

 

            //Creating object of MenuStrip class

            MnuStrip = newMenuStrip();

 

          

 

            String connectionString;

            connectionString = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;

            conn = newSqlConnection(connectionString);

            String Sequel = "SELECT MAINMNU,MENUPARVAL,STATUS FROM MNUPARENT";

            SqlDataAdapter da = newSqlDataAdapter(Sequel, conn);

            DataTable dt = newDataTable();

            conn.Open();

            da.Fill(dt);

 

            foreach (DataRow dr in dt.Rows)

            {

                MnuStripItem = newToolStripMenuItem(dr["MAINMNU"].ToString());

                 SubMenu(MnuStripItem, dr["MENUPARVAL"].ToString());

                MnuStrip.Items.Add(MnuStripItem);

            }

            // The Form.MainMenuStrip property determines the merge target.

            this.MainMenuStrip = MnuStrip;

            //Placing the control to the Form

            this.Controls.Add(MnuStrip);

        }

 

 

        publicvoid SubMenu(ToolStripMenuItem mnu, string submenu)

        {

            String Seqchild = "SELECT FRM_CODE,FRM_NAME,MNUSUBMENU FROM MNUSUBMENU WHERE MENUPARVAL='" + submenu + "'";

            SqlDataAdapter dachildmnu = newSqlDataAdapter(Seqchild, conn);

            DataTable dtchild = newDataTable();

            dachildmnu.Fill(dtchild);

 

            foreach (DataRow dr in dtchild.Rows)

            {

                ToolStripMenuItem SSMenu = newToolStripMenuItem(dr["FRM_NAME"].ToString(), null, newEventHandler(ChildClick));

                SubMenu(SSMenu, dr["MNUSUBMENU"].ToString());

                mnu.DropDownItems.Add(SSMenu);

            }

        }

 

        privatevoid ChildClick(object sender, EventArgs e)

        {

            // MessageBox.Show(string.Concat("You have Clicked ", sender.ToString(), " Menu"), "Menu Items Event",MessageBoxButtons.OK, MessageBoxIcon.Information);

 

            String Seqtx = "SELECT FRM_CODE FROM MNUSUBMENU WHERE FRM_NAME='" + sender.ToString() + "'";

            SqlDataAdapter datransaction = newSqlDataAdapter(Seqtx, conn);

            DataTable dtransaction = newDataTable();

            datransaction.Fill(dtransaction);

 

            Assembly frmAssembly = Assembly.LoadFile(Application.ExecutablePath);

            foreach (Type type in frmAssembly.GetTypes())

            {

                //MessageBox.Show(type.Name);

                if (type.BaseType == typeof(Form))

                {

                    if (dtransaction.Rows.Count > 0)

                    {

                        if (type.Name == dtransaction.Rows[0][0].ToString())

                        {

                            Form frmShow = (Form)frmAssembly.CreateInstance(type.ToString());

                            // then when you want to close all of them simple call the below code

 

                            foreach (Form form inthis.MdiChildren)

                            {

                                form.Close();

                            }

 

                            frmShow.MdiParent = this;

                            frmShow.WindowState = FormWindowState.Maximized;

                            //frmShow.ControlBox = false;

                            frmShow.Show();

                        }

                    }

                }

            }

        }

    }

}

 

Thanks for reading.

If you have any queries, Please feel free to post your comments and doubts.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By