Dynamic Menu Creation Problem
-
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); } } }
-
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); } } }
-
When I add a bookmark I want the dynamic menu to update and show the new bookmark without me having to restart the program.
-
When I add a bookmark I want the dynamic menu to update and show the new bookmark without me having to restart the program.
-
When I add a bookmark I want the dynamic menu to update and show the new bookmark without me having to restart the program.
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
-
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!
the code I posted does both.
-
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
What I am asking is how do I "repopulate" the menu?
-
the code I posted does both.
-
What I am asking is how do I "repopulate" the menu?
-
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!
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.
-
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.
anyone else have any ideas? I am stuck until I figure this out