Removing Context Menu Items
-
I have an Windows app that allows users to save "bookmarks", pages they have been, and then navigate back to them. That all works. In addition, we have restricted them to 15 bookmarks, and if they select a 16th, the first one in is popped out. I am attaching a Context Menu to a button on the toolbar. Now they would like to be able to delete an individual bookmark. I have the code working to dynamically add the bookmarked pages, to a sub-menu item for deletes. I even have the code working to delete the item from that MeunItems collection. When I check the count of that collection, it is correct, and when I click my code to add another bookmark, the old ones appear, and the one I deleted is gone. HOWEVER, when I go through the delete code, and then click on the button, none of the entries appear. Here's the code: Load the Menu Items, both in the Main menu and the Delete Bookmarks private void button1_Click(object sender, System.EventArgs e) { for (int i = 0; i < 6; i++) { MenuItem myMenuItem = mainToolBar.Buttons[0].DropDownMenu.MenuItems.Add("Added Item " + i); this.mainToolBar.Buttons[0].DropDownMenu.MenuItems[myMenuItem.Index].Click += new System.EventHandler(AnotherMenuTestMethod); deleteBookmarkMenuItem.MenuItems.Add(myMenuItem.CloneMenu()); deleteBookmarkMenuItem.MenuItems[deleteBookmarkMenuItem.MenuItems.Count -1].Click -= new System.EventHandler(AnotherMenuTestMethod); deleteBookmarkMenuItem.MenuItems[deleteBookmarkMenuItem.MenuItems.Count -1].Click += new System.EventHandler(DeleteMenuTestMethod); } } Here I'm trying to delete a specific one (just to see how to do it) public void DeleteMenuTestMethod(object sender, System.EventArgs e) { MenuItem myMenuItem = (MenuItem)sender; deleteBookmarkMenuItem.MenuItems.RemoveAt(3); MessageBox.Show(deleteBookmarkMenuItem.MenuItems.Count.ToString()); } As I mentioned the count is right, but when I click on the button, NOTHING appears in the Delete Items. By the way the 'deleteBookMarkMenuItem' variable is a global one for the form. I'm obviously missing something, anyone spot it? THANKS! WhiteWizard (aka Gandalf) -- modified at 8:09 Tuesday 2nd May, 2006
-
I have an Windows app that allows users to save "bookmarks", pages they have been, and then navigate back to them. That all works. In addition, we have restricted them to 15 bookmarks, and if they select a 16th, the first one in is popped out. I am attaching a Context Menu to a button on the toolbar. Now they would like to be able to delete an individual bookmark. I have the code working to dynamically add the bookmarked pages, to a sub-menu item for deletes. I even have the code working to delete the item from that MeunItems collection. When I check the count of that collection, it is correct, and when I click my code to add another bookmark, the old ones appear, and the one I deleted is gone. HOWEVER, when I go through the delete code, and then click on the button, none of the entries appear. Here's the code: Load the Menu Items, both in the Main menu and the Delete Bookmarks private void button1_Click(object sender, System.EventArgs e) { for (int i = 0; i < 6; i++) { MenuItem myMenuItem = mainToolBar.Buttons[0].DropDownMenu.MenuItems.Add("Added Item " + i); this.mainToolBar.Buttons[0].DropDownMenu.MenuItems[myMenuItem.Index].Click += new System.EventHandler(AnotherMenuTestMethod); deleteBookmarkMenuItem.MenuItems.Add(myMenuItem.CloneMenu()); deleteBookmarkMenuItem.MenuItems[deleteBookmarkMenuItem.MenuItems.Count -1].Click -= new System.EventHandler(AnotherMenuTestMethod); deleteBookmarkMenuItem.MenuItems[deleteBookmarkMenuItem.MenuItems.Count -1].Click += new System.EventHandler(DeleteMenuTestMethod); } } Here I'm trying to delete a specific one (just to see how to do it) public void DeleteMenuTestMethod(object sender, System.EventArgs e) { MenuItem myMenuItem = (MenuItem)sender; deleteBookmarkMenuItem.MenuItems.RemoveAt(3); MessageBox.Show(deleteBookmarkMenuItem.MenuItems.Count.ToString()); } As I mentioned the count is right, but when I click on the button, NOTHING appears in the Delete Items. By the way the 'deleteBookMarkMenuItem' variable is a global one for the form. I'm obviously missing something, anyone spot it? THANKS! WhiteWizard (aka Gandalf) -- modified at 8:09 Tuesday 2nd May, 2006
As it turns out, it appears this is a "bug" in .NET 2003, and has been fixed in 2005. I took the code as is into VS 2005 and it works fine. As we are moving there in the next month or so, I guess I'll just wait to finish this then. WhiteWizard (aka Gandalf)