Dynamic MenuStrip - Updated

Introduction:

In my previous article, I have shared how to create Dynamic Menustrip in C# Window Forms.

Source code:

In this blog, I have shared the source code to create Menustrip with Nested Sub-Menus.

Table Definition:

Table for Main Menu

 CREATE TABLE [dbo].[MNUPARENT](
[MAINMNU] [varchar](20),
[STATUS] [varchar](1),
[MENUPARVAL] [int] IDENTITY(1,1)
) 

Sample Insert Statements :

INSERT INTO MNUPARENT1([MAINMNU],[STATUS])
VALUES('Finance','Y')
INSERT INTO MNUPARENT1([MAINMNU],[STATUS])
VALUES('Inventory','Y')


Table for Sub-Menus :

 CREATE TABLE [dbo].[MNUSUBMENU](
[MENUPARVAL] [int],
[FRM_CODE] [varchar](50),
[FRM_NAME] [varchar](20),
[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')


Source 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
{
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,
MNUSUBMENUNAME 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();
}
} 
}
}
} 
}
}

Screen Shots:


Login.bmp

Nested Sub-Menus:

Nested Sub-Menu.bmp

When clicking Acc. Standards Sub-Menu 

Form for Nested Menu Acc. Standards.bmp

Thanks for Reading
Ebook Download
View all
Learn
View all