Dyanamic Creating Menulevels of Menustrip Items in C#

Posted by Nagarjunnatekar under C# on 2/3/2011 | Points: 10 | Views : 3780 | Status : [Member] | Replies : 3
How to convert a string variable to a Control type

Ex;
for(count 1 to 10)
{
String level= MenuLeveL+Count

ToolStripMenuItem level= new ToolStripMenuItem("MEnuName");

level-1.DropDownItems.Add(level);
}




Responses

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

Up
0
Down
Hi,

Can you breif your requirement... As far as i see the code, i understand you are creating a menu dynamically... From where you get the details of menu? in xml or db? we can customize it easily based on you requirement.

Thanks
Karthik
www.f5Debug.net

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

Posted by: Madhu.b.rokkam on: 2/3/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
DataTable menuDetails = null, parentMenuDetails = null, childMenuDetails = null;
MenuItem parentMenu, childMenu;

menuDetails = LoadDataFromDBorXML();

parentMenuDetails = menuDetails.Select("Filter Condition"); add your condition to filter out the parent menu records.

foreach(DataRow row in parentMenuDetails)
{
parentMenu = new MenuItem();
parentMenu.Text = row["Title"].ToString();
parentMenu.NavigateUrl = row["URL"].ToString();
//Like wise you can set the other parameters

childMenuDetails = menuDetails.Select("Filter Condition"); //Add the condition to filter the child menus for the parent based on some id.

foreach(DataRow row in childMenuDetails)
{
childMenu = new MenuItem();
childMenu = new MenuItem();
childMenu.Text = row["Title"].ToString();
childMenu.NavigateUrl = row["URL"].ToString();
//Like wise you can set the other parameters
parentMenu.ChildItems.Add(childMenu);
}
//Here mainMenu is the name of the menu control placed on the page.
mainMenu.Items.Add(parentMenu);
}



Thanks and Regards
Madhu

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

Posted by: Nagarjunnatekar on: 2/3/2011 [Member] Starter | Points: 25

Up
0
Down
in Datatable "DtSameMenuLevel" contains column "Menulevel" am getting Data like ( {1,1} {1,1,1},{1,2},{1,2,1},{1,2,2},{2},{2,1},{2,1,1},{2,2},{2,2,1},{2,2,2}) in Column "Menulevel"


for (int a = 0; a < DtSameMenuLevel.Rows.Count; a++)
{
ToolStripMenuItem MenuLevel=null;
string parentMenuLevel = DtSameMenuLevel.Rows[a]["Menulevel"].ToString();
string[] Menulevels = parentMenuLevel.Split(',');
// (Note: here in parentMenuLevel I will get MenuLevels like {1,1} {1,1,2} ,{1,1,2,1} )

if (((ICollection) Menulevels).Count == 1)
{
ParentMenuLevel = new ToolStripMenuItem(DtSameMenuLevel.Rows[a]["Name"].ToString());
MenuLevel.DisplayStyle = ToolStripItemDisplayStyle.Text;
if (mnuMainMenuLog != null)
mnuMainMenuLog.Items.Add(MenuLevel);
}
if (mnuMainMenuLog != null&& mnuMainMenuLog.Items.Count > 0 && ((ICollection) Menulevels).Count > 1)
{
ToolStripMenuItem MenuLevel1 = new ToolStripMenuItem(DtSameMenuLevel.Rows[a]["Name"].ToString());

MenuLevel1.Tag = DtSameMenuLevel.Rows[a]["Formname"].ToString();
MenuLevel1.Click += MenuClick;
MenuLevel1.DisplayStyle = ToolStripItemDisplayStyle.Text;
MenuLevel.DropDownItems.Add(MenuLevel1);
}
}

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

Login to post response