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. Dynamic Menu Creation Problem

Dynamic Menu Creation Problem

Scheduled Pinned Locked Moved C#
csharpxmlhelp
11 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.
  • J Offline
    J Offline
    jroberson10
    wrote on last edited by
    #1

    I am trying to teach myself C# and I have started writing a web browser based on mozilla gecko. Right now I am working on storing and retrieving bookmarks from an xml document. I can save and read the bookmarks just fine. However in order to see a bookmark I just added I must exit the program and restart it. I have tried calling the function that populates the menu after the new bookmark is written to the xml file but it runs and loads everything but the new bookmark. Below is the code I use to populate the menus.

    public void PopulateBM()

        {
    
    
            string filename = "bookmarks.xml";
    
    
            if(File.Exists(filename))
    
    
            {
    
    
             XmlDocument xmldoc = new XmlDocument();
    
    
             xmldoc.Load(filename);
    
    
             XmlNodeList xmlnode = xmldoc.GetElementsByTagName("Bookmark");
    
    
             for (int i = 0; i < xmlnode.Count; i++)
    
    
                {
    
    
                 ToolStripMenuItem item = new ToolStripMenuItem();
    
    
                 item.Text = xmlnode\[i\].FirstChild.InnerText;
    
    
                 item.Tag = xmlnode\[i\].LastChild.InnerText;
    
    
                 item.ToolTipText = xmlnode\[i\].LastChild.InnerText;
    
    
                 item.Click += new EventHandler(this.item\_Click);
    
    
                 this.bookmarksToolStripMenuItem.DropDownItems.Insert(3,item);
    
    
                 }
    
    
            }
    
    
          }
    
    L 1 Reply Last reply
    0
    • J jroberson10

      I am trying to teach myself C# and I have started writing a web browser based on mozilla gecko. Right now I am working on storing and retrieving bookmarks from an xml document. I can save and read the bookmarks just fine. However in order to see a bookmark I just added I must exit the program and restart it. I have tried calling the function that populates the menu after the new bookmark is written to the xml file but it runs and loads everything but the new bookmark. Below is the code I use to populate the menus.

      public void PopulateBM()

          {
      
      
              string filename = "bookmarks.xml";
      
      
              if(File.Exists(filename))
      
      
              {
      
      
               XmlDocument xmldoc = new XmlDocument();
      
      
               xmldoc.Load(filename);
      
      
               XmlNodeList xmlnode = xmldoc.GetElementsByTagName("Bookmark");
      
      
               for (int i = 0; i < xmlnode.Count; i++)
      
      
                  {
      
      
                   ToolStripMenuItem item = new ToolStripMenuItem();
      
      
                   item.Text = xmlnode\[i\].FirstChild.InnerText;
      
      
                   item.Tag = xmlnode\[i\].LastChild.InnerText;
      
      
                   item.ToolTipText = xmlnode\[i\].LastChild.InnerText;
      
      
                   item.Click += new EventHandler(this.item\_Click);
      
      
                   this.bookmarksToolStripMenuItem.DropDownItems.Insert(3,item);
      
      
                   }
      
      
              }
      
      
            }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ...And your problem exactly is?:confused:

      Life is a stage and we are all actors!

      J 1 Reply Last reply
      0
      • L Lost User

        ...And your problem exactly is?:confused:

        Life is a stage and we are all actors!

        J Offline
        J Offline
        jroberson10
        wrote on last edited by
        #3

        When I add a bookmark I want the dynamic menu to update and show the new bookmark without me having to restart the program.

        L 0 2 Replies Last reply
        0
        • J jroberson10

          When I add a bookmark I want the dynamic menu to update and show the new bookmark without me having to restart the program.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You should post the code you are using to update the bookmarks,not the code used to load from the file. :)

          Life is a stage and we are all actors!

          J 1 Reply Last reply
          0
          • J jroberson10

            When I add a bookmark I want the dynamic menu to update and show the new bookmark without me having to restart the program.

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            Try looking into a FileSystemWatcher instance. Set it to the file's directory, and every time you get told that a file has changed, compare the changed filename to "Bookmarks.xml" or whatever you've called your bookmarks file. If your file has changed, then repopulate the menu. Alternatively, you could add an item to the menu every time you bookmark something. That would be a lot simpler, but would only work if your application was the only one using the file

            Between the motion And the act Falls the Shadow

            J 1 Reply Last reply
            0
            • L Lost User

              You should post the code you are using to update the bookmarks,not the code used to load from the file. :)

              Life is a stage and we are all actors!

              J Offline
              J Offline
              jroberson10
              wrote on last edited by
              #6

              the code I posted does both.

              L 1 Reply Last reply
              0
              • 0 0x3c0

                Try looking into a FileSystemWatcher instance. Set it to the file's directory, and every time you get told that a file has changed, compare the changed filename to "Bookmarks.xml" or whatever you've called your bookmarks file. If your file has changed, then repopulate the menu. Alternatively, you could add an item to the menu every time you bookmark something. That would be a lot simpler, but would only work if your application was the only one using the file

                Between the motion And the act Falls the Shadow

                J Offline
                J Offline
                jroberson10
                wrote on last edited by
                #7

                What I am asking is how do I "repopulate" the menu?

                0 1 Reply Last reply
                0
                • J jroberson10

                  the code I posted does both.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You just need to update the menu in the same manner as you used to populate it from file.You really needn't to use logic based on FileSystemWatcher.

                  Life is a stage and we are all actors!

                  J 1 Reply Last reply
                  0
                  • J jroberson10

                    What I am asking is how do I "repopulate" the menu?

                    0 Offline
                    0 Offline
                    0x3c0
                    wrote on last edited by
                    #9

                    You would repopulate it using the same method you populated it to start with

                    Between the motion And the act Falls the Shadow

                    1 Reply Last reply
                    0
                    • L Lost User

                      You just need to update the menu in the same manner as you used to populate it from file.You really needn't to use logic based on FileSystemWatcher.

                      Life is a stage and we are all actors!

                      J Offline
                      J Offline
                      jroberson10
                      wrote on last edited by
                      #10

                      Thats just it. When I run the code again nothing happens. It completes and the menu stays the same. I tested this by not populating it on application start and only trying to populate after adding a new bookmark. When I do that the menu stays empty.

                      J 1 Reply Last reply
                      0
                      • J jroberson10

                        Thats just it. When I run the code again nothing happens. It completes and the menu stays the same. I tested this by not populating it on application start and only trying to populate after adding a new bookmark. When I do that the menu stays empty.

                        J Offline
                        J Offline
                        jroberson10
                        wrote on last edited by
                        #11

                        anyone else have any ideas? I am stuck until I figure this out

                        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