Home > DeveloperSection > Blogs > MenuStrip with Nested Submenus

MenuStrip with Nested Submenus


C# C# 
Ratings:
0 Comment(s)
 3674  View(s)
Rate this:

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:

CREATE TABLE [dbo].[MNUPARENT](

                [MENUPARVAL] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,

                [MENUNAME] [varchar](20) NOT NULL,

                [STATUS] [varchar](1));

Sample Insert Statements:

INSERT INTO MNUPARENT(MENUNAME,STATUS)

VALUES('Offline','Y');

 

INSERT INTO MNUPARENT(MENUNAME,STATUS)

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

 

INSERT INTO MNUPARENT(MENUNAME,STATUS)

VALUES('Administration','Y');

 

Child Menu:

CREATE TABLE [dbo].[MNUSUBMENU](

                [MENUPARVAL] [int] NOT NULL,

                [FRM_CODE] [varchar](50),

                [FRM_NAME] [varchar](20) NOT NULL,

                [MNUSUBMENU] [int] NOT NULL PRIMARY KEY,

                [STATUS] [varchar](1))

Sample Insert Statements:

 

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

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

 

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

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

 

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

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

 

INSERT INTO [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

{

    public partial class FrmParent : Form

    {

        SqlConnection conn;

        MenuStrip MnuStrip;

        ToolStripMenuItem MnuStripItem;

      

        public FrmParent()

        {

            InitializeComponent();

        }

 

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

 

          

 

            String connectionString;

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

            conn = new SqlConnection(connectionString);

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

            SqlDataAdapter da = new SqlDataAdapter(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;

            //Placing the control to the Form

            this.Controls.Add(MnuStrip);

        }

 

 

        public void SubMenu(ToolStripMenuItem mnu, string submenu)

        {

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

            SqlDataAdapter dachildmnu = new SqlDataAdapter(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));

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

                mnu.DropDownItems.Add(SSMenu);

            }

        }

 

        private void 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 = new SqlDataAdapter(Seqtx, conn);

            DataTable dtransaction = new DataTable();

            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 in this.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.


Don't want to miss updates? Please click the below button!

Follow MindStick