Create a Menu and Sub Menu(s) in run time using C#

T.saravanan
Posted by T.saravanan under Windows Forms category on | Points: 40 | Views : 19481
Hi All,

Here i am given a code snippet for How to Create a Menu in Forms in run time using C#.


//Assign Globally following Objects
MenuStrip menu = new MenuStrip();
DataTable dt=new DataTable();
dt -->Have Menu Details.

private void button1_Click(object sender, EventArgs e)
{
for (int nMain = 0; nMain < dt.Rows.Count; nMain++)
{
ToolStripMenuItem tsmMain=new ToolStripMenuItem(dt.Rows[nMain]["Menu Name"].ToString());
tsmMain.DisplayStyle=ToolStripItemDisplayStyle.Text;
menu.Items.Add(tsmMain);
AddSubMenu(dt.Rows[nMain]["Menu Id"].ToString(),tsmMain);
}
this.Controls.Add(menu);
}

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.DisplayStyle=ToolStripItemDisplayStyle.Text;
main.DropDownItems.Add(tsmSub);
AddSubMenu(dtChild.Rows[nSub]["Menu Id"].ToString(),tsmSub);
}
}
}


I hope this Code very useful.

Cheers :)

Comments or Responses

Posted by: Leokady on: 12/13/2010 Level:Starter | Status: [Member] | Points: 10
Hi, Saravanan
Great code. You save all menus in tables. I would like to restore from table at same time the name all my forms from windows application. How to run forms when I will click on the dynamically created menu. The type of field which keep the name of forms is character. Can you just send me small example.
Thanks . leokady@gmail.com
Posted by: T.saravanan on: 12/15/2010 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi ALL,

@Leokady requirement i have added the code for how to open a form when i click a menu in Main form.

Bold lines are extra added in below method and also call one event for open the form.


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


Cheers :)

Posted by: Leokady on: 12/18/2010 Level:Starter | Status: [Member] | Points: 10
Hi ,T.Saravanan
I tested code works very well. I have just one problem. I try to reload menu several times(by click on the button) and menu duplicated. I try to use despose method,remove method and it doesnot work:
if (menu != null)
{
this.menu.Dispose();
//this.Controls.Remove(menu);
this.Refresh();
}
How ,actually completely remove menustrip from form and load again w/o duplication or load other menu to same menu object ,but from other table.
thanks, leokady@gmail.com


Posted by: Leokady on: 12/18/2010 Level:Starter | Status: [Member] | Points: 10
Hi ,T.Saravanan
I tested code works very well. I have just one problem. I try to reload menu several times(by click on the button) and menu duplicated. I try to use despose method,remove method and it doesnot work:
if (menu != null)
{
this.menu.Dispose();
//this.Controls.Remove(menu);
this.Refresh();
}
How ,actually completely remove menustrip from form and load again w/o duplication or load other menu to same menu object ,but from other table.
thanks, leokady@gmail.com


Posted by: Asus on: 10/6/2013 Level:Starter | Status: [Member] | Points: 10
i would appreciate if you could post a demo project with table structure also

Login to post response