Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event handler for menu populated from database

Event handler for menu populated from database

Scheduled Pinned Locked Moved C#
databasehelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    random4
    wrote on last edited by
    #1

    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

    M L 2 Replies Last reply
    0
    • R random4

      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

      M Offline
      M Offline
      Mark J Miller
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • R random4

        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

        L Offline
        L Offline
        lsconyer
        wrote on last edited by
        #3

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

        Lester http://www.lestersconyers.com

        1 Reply Last reply
        0
        • M Mark J Miller

          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

          R Offline
          R Offline
          random4
          wrote on last edited by
          #4

          Thanks for your suggestion.. it helped! :-D

          adi_nik

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups