Introduction:
Source code:
In this blog, I have shared the source code to create Menustrip with Nested Sub-Menus.
Table Definition:
Table for Main Menu
|
Sample Insert Statements :
|
Table for Sub-Menus :
|
Sample Insert Statements:
|
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:

Nested Sub-Menus:

When clicking Acc. Standards Sub-Menu

Thanks for Reading

adul moolkhuntodPosted Aug 8, 2017, 5:24 AM
For VB.Net ???. please
salman beheraPosted Oct 31, 2014, 11:19 AM
hi i using your code but static variable value not getting in another page.static variable in my login page and i use this variable in all other page.but value i get null in other page..i using code like public static string FiancialYear = ddlfinacialyear.Text.ToString(); lblFnyr.Text = "Financial Year:" + frm_UserLogin.FiancialYear;(other page) but value null here.is there any solution??