Event handler for menu populated from database
-
Hi, i have populated the menu in a Panel from database :-D . I want to know how do i catch the clicks on the menuitems :(( Below is the code for generating and populating menu items in a Panel. Please help!
private void PopulateMenu() { DataSet ds = GetDataSetForMenu(); Menu menu = new Menu(); foreach (DataRow parentItem in ds.Tables["Categories"].Rows) { MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]); menu.Items.Add(categoryItem); foreach (DataRow childItem in parentItem.GetChildRows("Children")) { MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]); categoryItem.ChildItems.Add(childrenItem); } } Panel1.Controls.Add(menu); Panel1.DataBind(); } private DataSet GetDataSetForMenu() { SqlConnection myConnection = new SqlConnection(GetConnectionString()); SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM Categories", myConnection); SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM Products", myConnection); DataSet ds = new DataSet(); adCat.Fill(ds, "Categories"); adProd.Fill(ds, "Products"); ds.Relations.Add("Children", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]); return ds; }
Thanks in advance for your help!adi_nik
-
Hi, i have populated the menu in a Panel from database :-D . I want to know how do i catch the clicks on the menuitems :(( Below is the code for generating and populating menu items in a Panel. Please help!
private void PopulateMenu() { DataSet ds = GetDataSetForMenu(); Menu menu = new Menu(); foreach (DataRow parentItem in ds.Tables["Categories"].Rows) { MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]); menu.Items.Add(categoryItem); foreach (DataRow childItem in parentItem.GetChildRows("Children")) { MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]); categoryItem.ChildItems.Add(childrenItem); } } Panel1.Controls.Add(menu); Panel1.DataBind(); } private DataSet GetDataSetForMenu() { SqlConnection myConnection = new SqlConnection(GetConnectionString()); SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM Categories", myConnection); SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM Products", myConnection); DataSet ds = new DataSet(); adCat.Fill(ds, "Categories"); adProd.Fill(ds, "Products"); ds.Relations.Add("Children", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]); return ds; }
Thanks in advance for your help!adi_nik
Have you looked for a click event? http://msdn2.microsoft.com/en-us/library/system.windows.forms.menuitem.click(VS.71).aspx[^]
MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]); chilrenItem.Click += new System.EventHandler(this.menuitem_click); private void menuitem_click(Object sender, System.EventArgs e){ //respond to the click event }
Mark's blog: developMENTALmadness.blogspot.com
-
Hi, i have populated the menu in a Panel from database :-D . I want to know how do i catch the clicks on the menuitems :(( Below is the code for generating and populating menu items in a Panel. Please help!
private void PopulateMenu() { DataSet ds = GetDataSetForMenu(); Menu menu = new Menu(); foreach (DataRow parentItem in ds.Tables["Categories"].Rows) { MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]); menu.Items.Add(categoryItem); foreach (DataRow childItem in parentItem.GetChildRows("Children")) { MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]); categoryItem.ChildItems.Add(childrenItem); } } Panel1.Controls.Add(menu); Panel1.DataBind(); } private DataSet GetDataSetForMenu() { SqlConnection myConnection = new SqlConnection(GetConnectionString()); SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM Categories", myConnection); SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM Products", myConnection); DataSet ds = new DataSet(); adCat.Fill(ds, "Categories"); adProd.Fill(ds, "Products"); ds.Relations.Add("Children", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]); return ds; }
Thanks in advance for your help!adi_nik
I've never heard of a the Menu class. I've heard of MainMenu, MenuStrip, ContextMenu, ToolStrip, but not that. Anyway, if its anything like the previous three components, you can capture the click events on the items themselves like someone already mentioned. Or you can handle the MenuItemsClicked event of the menu. Heres an example from a toolstrip:
ToolbarControl.ItemClicked += new ToolStripItemClickedEventHandler(ToolbarControl_ItemClicked); private void ToolbarControl_ItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e) { //Get the name of the item clicked switch (e.ClickedItem.Name) { case "Item1": DoWork(); break; case "AnotherItem": DoSomethingElse(); break; } }
-
Have you looked for a click event? http://msdn2.microsoft.com/en-us/library/system.windows.forms.menuitem.click(VS.71).aspx[^]
MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]); chilrenItem.Click += new System.EventHandler(this.menuitem_click); private void menuitem_click(Object sender, System.EventArgs e){ //respond to the click event }
Mark's blog: developMENTALmadness.blogspot.com