Dynamically Creating Menu,SubMenu of Menustrip in C# WinForm

Posted by Nagarjunnatekar under ASP.NET on 12/27/2010 | Points: 10 | Views : 28455 | Status : [Member] | Replies : 5
I,Want to Dynamically Create a MenuStrip Like Below
------------------------------------------------------------------------------------------------
MENU
|
|--->SubMenu1---->|-->Submenu_G
|
|
| |-->Submenu_A
|--->SubMenu2---->|-->Submenu_B
| |-->Submenu_C
|
|
|--->SubMenu3--->|-->Submenu_D
| |-->Submenu_E
|

------------------------------------------------------------------------------------------------

Plz ,May Any Body Help Me To Resolve This....!

Thank You in Advance




Responses

Posted by: T.saravanan on: 12/27/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Nagarjunnatekar,

I had written a code for creating menus in runtime.

Refer this link...http://www.dotnetfunda.com/codes/code971-create-a-menu-and-sub-menus-in-run-time-using-csharp.aspx

I think its useful to you...

Cheers :)

Thanks,
T.Saravanan

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

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

Up
0
Down
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 ,where I Got struck is in Creating 'Submenu_A' and 'Submenu_B'
i.e as below
-------------------------------------------------------------------------------

Menu
|
|
| |--->Submenu_A
|-->SubMenu1--> |--->Submenu_B
|
|
|
------------------------------------------------------------------------------------------------------

Plz do reply....!

Thank You


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

Posted by: T.saravanan on: 12/28/2010 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Are you maintain any parentId column in your datatable for creating submenu? what do you try yourself can you show yourcode here ?

For ex: DataTable struture
Menu_Id Display_Name Form_Name Parent_Id
1 A frm_1 0
2 B frm_2 1
3 C frm_3 1
4 D frm_4 2
5 E frm_5 2
6 F frm_6 0
7 G frm_7 0
8 H frm_8 6
9 I frm_9 6
10 J frm_10 7

Output Struture:

Main
|-->A ----> B ---->D
| |--> C |--->E
|
|--->F---> H
| |--> I
|--->G ---> J

First to create a proper table and then to pass that table in above coding...
Try this if you struck ask your doubt...

Cheers :)

Thanks,
T.Saravanan

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

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: Hornstalk on: 12/10/2013 [Member] Starter | Points: 25

Up
0
Down
Design custom menu, sub menus in C# Windows Forms,
http://www.kettic.com/winforms_ui/csharp_guide/menu_designer.shtml

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

Login to post response