articles

Home / DeveloperSection / Articles / Dynamic MenuStrip in Windows Forms

Dynamic MenuStrip in Windows Forms

Shankar M19994 11-Feb-2013

Dynamic MenuStrip in Windows Forms

Introduction

 

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

Using the Code

1. Create a new project and give it a name DynamicMenuStripDBDriven.

Dynamic MenuStrip in Windows Forms

2. Rename the Form1 to FrmParent, which is the parent window as we will be dealing with multiple forms.

MenuStrip


The MenuStrip gives an easier approach to represent and group application commands.

 

To add a MenuStrip Dynamically at run-time we create an instance of the MenuStrip Class.

 

//Creating object of MenuStrip class

 

 MenuStrip MnuStrip = new MenuStrip ();

 

To add the MenuStrip control to the Form, we use the Add property


 this.Controls.Add (MnuStrip);

 

Here we will be working with Child Forms, so set it IsMdiContainer property to true.

 

// To make the Form as the Parent Form

 this.IsMdiContainer = true;

 

Code:

           

this.IsMdiContainer = true;

            MenuStrip MnuStrip = newMenuStrip();

            this.Controls.Add(MnuStrip);

 

After executing the piece of code in the Form load event , our Form looks something like this,

Dynamic MenuStrip in Windows Forms

 

Figure 1: Placing the MenuStrip control in the Form

 

3. A MenuStrip is nothing without MenuItems. To add MenuItems to the MenuStrip we use the ToolStripMenuItem class.

In this for instance we have created Tables as we need to Load the Menu Items Dynamically:

 To Load the Main Menu :

 

CREATETABLE [dbo].MNU_PARENT(

            MAINMNU varchar(20),

            STATUS  varchar(1),

            MENUPARVAL int

) 

 

Sample Insert statement to Load the Parent Menu:

INSERTINTO MNU_PARENT(MAINMNU,STATUS,MENUPARVAL)

VALUES('Transactions','Y',1)

 

INSERTINTO MNU_PARENT(MAINMNU,STATUS,MENUPARVAL)

VALUES('Inventory','Y',2)

 

To Load the Sub Menus to Form:

 

CREATETABLE [dbo].[MNU_SUBMENU](

            MENUPARVAL int,

            FRM_CODE varchar(50),

            FRM_NAME varchar(20),

            STATUS varchar(1)

)

 

Sample Insert statement to MNU_SUBMENU table to Load Submenus to the Form

INSERTINTO MNU_SUBMENU(MENUPARVAL,FRM_CODE,FRM_NAME,STATUS)

VALUES(1,'FrmAccount','Accounting','Y')

 

 

INSERTINTO MNU_SUBMENU(MENUPARVAL,FRM_CODE,FRM_NAME,STATUS)

VALUES(1,'FrmFinance','Finance','Y')

 

 

INSERTINTO MNU_SUBMENU(MENUPARVAL,FRM_CODE,FRM_NAME,STATUS)

VALUES(1,null,'Despatch','Y')

 

Here, the MENUPARVAL value in Table MNU_SUBMENU is 1 as this will be the submenu for Transactions Menu.

 4. To Start with add 2 Windows Forms to the Project using Project -> Add Windows Form and rename it and create a structure as shown below

Dynamic MenuStrip in Windows Forms

Figure 2: Structure of the Project

5. The ToolStripMenuItem shows differently depending on whether it is a Top-level Menu Item in 
a MenuStrip Control or a sub menu.

When it a Sub-Menu it shows a margin to the left where images are displayed if we have added.

Menu Items are added to the MenuStrip using the Items Collection and we can also add properties
for handling shortcut keys and supporting checked menu items.

In the Form Load event Paste the below code:

privatevoid FrmParent_Load(object sender, EventArgs e)

        {

            // To make this Form the Parent Form

            this.IsMdiContainer = true;

 

            //Creating object of MenuStrip class

            MnuStrip = newMenuStrip();

 

            //Placing the control to the Form

            this.Controls.Add(MnuStrip);

 

            String connectionString;

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

            conn = new SqlConnection(connectionString);

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

            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;

        }

 

Dynamic MenuStrip in Windows Forms

Figure 3: Loading the Main Menu

In the above code, we read the Settings of the Configuration File and load the Parent Menu,

6. Adding Submenus:

To add Sub Menu to the Transactions Menu, we use the ToolStripMenuItem Class I have included this piece of Code to Load the SubMenu.

Method:

publicvoid SubMenu(ToolStripMenuItem mnu, string submenu)

        {

            String Seqchild = "SELECT FRM_NAME FROM MNU_SUBMENU WHERE MENUPARVAL='" + submenu + "'";

            SqlDataAdapter dachildmnu = newSqlDataAdapter(Seqchild, conn);

            DataTable dtchild = newDataTable();

            dachildmnu.Fill(dtchild);

 

            foreach (DataRow dr in dtchild.Rows)

            {

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

                mnu.DropDownItems.Add(SSMenu);

            }

        }

 

The above piece of code loads the SubMenus for each Parent. Here we loop each form and registerd the events with the ChildClick.

7. Child Click Event:

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 MNU_SUBMENU 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 (type.Name == dtransaction.Rows[0][0].ToString())

                    {

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

                        // then we  close all of the child Forms with  simple below code

 

                        foreach (Form form inthis.MdiChildren)

                        {

                            form.Close();

                        }

 

                        frmShow.MdiParent = this;

                        frmShow.WindowState = FormWindowState.Maximized;

                        //frmShow.ControlBox = false;

                        frmShow.Show();

                    }

                }

            }

        }

 

Here,

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


To get the FRM_CODE (i.e.,) the Form name.

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

 

Here we add the namespace System.Reflection to our project which contains classes that allow us to obtain information about the application dynamically at run-time.

The classes that provide access to the metadata of the running program are available in the

System.Reflection namespace.

type.BaseType == typeof(Form)

 

Comparing the BaseType of the application to Form and get the Type from which System.Type is Inherited

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

 

Comparing the Name of the Form with the Form that is present in the Database.

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

 

Creating Instance of the Form by Locating the specified type from the Assembly.

Entire code:

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

 

            //Placing the control to the Form

            this.Controls.Add(MnuStrip);

 

            String connectionString;

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

            conn = new SqlConnection(connectionString);

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

            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;

        }

 

 

        publicvoid SubMenu(ToolStripMenuItem mnu, string submenu)

        {

            String Seqchild = "SELECT FRM_NAME FROM MNU_SUBMENU WHERE MENUPARVAL='" + submenu + "'";

            SqlDataAdapter dachildmnu = newSqlDataAdapter(Seqchild, conn);

            DataTable dtchild = newDataTable();

            dachildmnu.Fill(dtchild);

 

            foreach (DataRow dr in dtchild.Rows)

            {

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

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

                    }

                }

            }

        }

    }

}

 

When we run the Project,

Dynamic MenuStrip in Windows Forms

Clicking the Accounting Form:

Dynamic MenuStrip in Windows Forms

Clicking the Finance Transaction:

Dynamic MenuStrip in Windows Forms

Summary:

In this article we have discussed how to create a Database Driven MenuStrip in 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.


Updated 21-Jan-2020

Leave Comment

Comments

Liked By