How to Create Menu,SubMenu of Menustrip Dynamically in c# Winform Application

Posted by Nagarjunnatekar under C# on 12/28/2010 | Points: 10 | Views : 32549 | Status : [Member] | Replies : 3
private void AddSubMenu(string sId, ToolStripMenuItem main)

{

// Filter a Sub Menu Fields from a Main Table

DataRow[ ] drFilter;

DataTable dtChild=new DataTable();

dtChild=dt.Clone();

drFilter = dt.Select("[Menu Id]="'"+sId+"'");

foreach(DataRow dr in drFilter)

{

dtChild.ImportRow(dr);

}

//Add Sub Menu into related Main Menu

for (int nSub = 0; nSub < dtChild.Rows.Count; nSub++)

{

ToolStripMenuItem tsmSub=new ToolStripMenuItem(dtChild.Rows[nSub]["Menu Name"].ToString());

tsmSub.Tag = dtChild.Rows[nSub]["Form Name"].ToString(); // Here form name means "frmMaster" you have manage the form name in your database (or) datatable.

tsmSub.Click +=new EventHandler(MenuClick);

tsmSub.DisplayStyle=ToolStripItemDisplayStyle.Text;

main.DropDownItems.Add(tsmSub);

AddSubMenu(dtChild.Rows[nSub]["Menu Id"].ToString(),tsmSub);

}

}



Private void MenuClick (object sender, EventArgs e)

{

string sFormName = "Your UI Namespace."+((ToolStripMenuItem)sender).Tag.ToString());

System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

Form frm = (Form)asm.CreateInstance(sFormName );

frm.MdiParent = this;

frm.StartPosition = FormStartPosition.CenterScreen;

frm.Show();

}


I already Tried Above one ,But where I Got struck is in Creating 'Submenu_A' and 'Submenu_P'
i.e as below
-------------------------------------------------------------------------------

Menu
|
|
|
|-->SubMenu1 ----> |--->Submenu_A ---->|----->Submenu_P
|
|
|
------------------------------------------------------------------------------------------------------

Plz do reply....!

Thank You




Responses

Posted by: Nagarjunnatekar on: 12/28/2010 [Member] Starter | Points: 25

Up
0
Down
Hi I Have Attached Doc Plz Go thru it Plz......!
 Download source file

Nagarjunnatekar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/6/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

try this link below

http://msdn.microsoft.com/en-us/library/aa984319%28v=vs.71%29.aspx

Thanks
Karthik
www.f5Debug.net

Nagarjunnatekar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Soleh on: 7/19/2017 [Member] Starter | Points: 25

Up
0
Down
DOWNLOAD FULL PROJECT https://drive.google.com/file/d/0B55z6hBNJXFEbklpWWlXOVJsbkk/view

Nagarjunnatekar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response