public class FavoritesMenuItem : MenuItem { protected string URL = ""; public FavoritesMenuItem(string text, string link) : base(text) { this.Click += new System.EventHandler(this.ClickHandler); this.URL = link; // this is where you'd do icons too, I don't have any } #region Events protected virtual void ClickHandler(object sender, System.EventArgs e) { // code here for handling the menu item click to browse the URL } #endregion }Then of course, you'd add them like this:
private void TestMenuAddItemsDynamically() { MenuItem item; // leave it this way so you can have other item sub-classes foreach (DataRow row in this.oData.Rows) { if ((bool)row["IsFavorites"] == true) { item = new FavoritesMenuItem(row["description"].ToString(), row["url"].ToString()); this.AddMenuItem("Favorites", item); } else { item = new MyOtherMenuItem(row["description"].ToString()); this.AddMenuItem("Dynamic Menu", item); } } }Does this help?
>>private void TestMenuAddItemsDynamically() >>{ >> MenuItem item; >> for (int i = 0; i < this.oData.Rows.Count; i++) >> { >> item = new MenuItem(this.oData.Rows[i]["description"].ToString()); >> this.AddMenuItem("Dynamic Menu", item); >> } >>} >>// Generic method for adding to any Menu >>private void AddMenuItem(string name, MenuItem item) >>{ >> string lookForName = name.Replace("&", ""); >> >> // Find top-level menu item >> MenuItem addTo = null; >> for (int i = 0; i < this.Menu.MenuItems.Count; i++) >> { >> MenuItem topLevel = this.Menu.MenuItems[i]; >> if (string.Compare(topLevel.Text.Replace("&", ""), lookForName, true) == 0) >> { >> addTo = topLevel; >> break; >> } >> } >> >> // Was a top-level menu item found? >> if (addTo == null) >> { >> addTo = new MenuItem(name); >> this.Menu.MenuItems.Add(addTo); >> addTo.Index = this.Menu.MenuItems.Count - 2; >> } >> >> addTo.MenuItems.Add(item); >>} >>>>