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. Newbie: Best way to populate a context menu from DB?

Newbie: Best way to populate a context menu from DB?

Scheduled Pinned Locked Moved C#
questiondatabasetutorial
5 Posts 4 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.
  • P Offline
    P Offline
    Phillip Hodges
    wrote on last edited by
    #1

    What is the best way to populate a context menu with data from a DB when it is clicked on? Basically I am trying to create a context menu from a notifyIcon and populate it with names & numbers when it is right clicked... I can get the data from the DB, I just need to know how to put this data into the menu... Any ideas or pointers? Thanks in advance, Phil

    E 1 Reply Last reply
    0
    • P Phillip Hodges

      What is the best way to populate a context menu with data from a DB when it is clicked on? Basically I am trying to create a context menu from a notifyIcon and populate it with names & numbers when it is right clicked... I can get the data from the DB, I just need to know how to put this data into the menu... Any ideas or pointers? Thanks in advance, Phil

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      You can dynamically create the context menu when the click is performed. Also you can use one context menu and just clear the menu items and add new items at runtime. The code below is .NET 2 same logic different control in .NET 1.1

      contextMenuStrip1.Items.Clear();
      ToolStripItem ts = null;
      for(int i=0;i<5;i++){
      ts = contextMenuStrip1.Items.Add(i.ToString());
      }
      contextMenuStrip1.Show();

      A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

      A 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        You can dynamically create the context menu when the click is performed. Also you can use one context menu and just clear the menu items and add new items at runtime. The code below is .NET 2 same logic different control in .NET 1.1

        contextMenuStrip1.Items.Clear();
        ToolStripItem ts = null;
        for(int i=0;i<5;i++){
        ts = contextMenuStrip1.Items.Add(i.ToString());
        }
        contextMenuStrip1.Show();

        A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

        A Offline
        A Offline
        Anil Ch
        wrote on last edited by
        #3

        We can create context menu in this way but also we need to handle events for these context menus. To do so we can create a common event for all menu items and then based on the context name respective code can be executed. Impossible itself says I M Possible.

        E L 2 Replies Last reply
        0
        • A Anil Ch

          We can create context menu in this way but also we need to handle events for these context menus. To do so we can create a common event for all menu items and then based on the context name respective code can be executed. Impossible itself says I M Possible.

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          duh

          private void button1_Click(object sender, EventArgs e) {
          contextMenuStrip1.Items.Clear();
          ToolStripItem ts = null;
          for(int i=0;i<5;i++){
          ts = contextMenuStrip1.Items.Add(i.ToString());
          ts.Click += new EventHandler(ts_Click);
          ts.Tag = [PUT SOMETHING HERE TO ID IT]
          }
          contextMenuStrip1.Show();
          }

          private void ts_Click(object sender, EventArgs e){
          ...
          }

          A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

          1 Reply Last reply
          0
          • A Anil Ch

            We can create context menu in this way but also we need to handle events for these context menus. To do so we can create a common event for all menu items and then based on the context name respective code can be executed. Impossible itself says I M Possible.

            L Offline
            L Offline
            LongRange Shooter
            wrote on last edited by
            #5

            All you have to do is bind your events at the time you create each context menu item. You would basically be doing in abstract code, what the designer does with hard coded code.

            foreach ( string element in myListofThings )
            {
            ToolStripMenuItem menu = new ToolStripMenuItem();
            menu.Name = element.ToLower();
            menu.Text = element;
            menu.Click+=new SystemEventHandler(HandleContextClick);
            myContextMenu.Add( menu );
            foreach ( string subElement in SubElementList )
            {
            ToolStripMenuItem submenu = new ToolStripMenuItem();
            blah blah blah
            submenu.Click+=new SystemEventHandler(HandleSubContextClick);
            }
            }

            :suss:

            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