Resolved Adding a new level to Context Menu

Asismey

New member
Joined
Aug 26, 2023
Messages
3
Programming Experience
Beginner
Hi,

I have written a Taskbar Windows Form which has a Menu of Items

Menu Item
Menu Item
Menu Item - Submenu -Item1
- Submenu -Item2

I want to add a level off the submenu, but I just cant get it to work

Menu Item
Menu Item
Menu Item - Submenu -Item1
- Submenu -Item2 - SubMenu - Item1

My code is

C#:
                MenuItem Spacer3 = (new MenuItem("-"));
                MenuItem UpDateAsset = (new MenuItem("Location", new EventHandler(this.ShowMyLocation)));
                MenuItem Spacer2 = (new MenuItem("-"));
                MenuItem MyComputer = new MenuItem("Config", new EventHandler(this.ShowConfig));
                MenuItem Spacer4 = (new MenuItem("-"));
                MenuItem MenuMessage = new MenuItem("Toast", new EventHandler(this.ShowToast));
                MenuItem Spacer = new MenuItem("-");
                MenuItem AdminItem = new MenuItem("Admin");

                AdminItem.MenuItems.Add(new MenuItem("Refresh", new EventHandler(this.RefreshAll)));
                AdminItem.MenuItems.Add(new MenuItem("Update", new EventHandler(this.UpdateAll)));
                AdminItem.MenuItems.Add(new MenuItem("Exit", new EventHandler(this.Exit)));




                MenuItem ToolsItem = new MenuItem("User Tools");

                ToolsItem.MenuItems.Add(new MenuItem("Software", new EventHandler(this.ShowSoftware)));
                ToolsItem.MenuItems.Add(new MenuItem("-"));
                ToolsItem.MenuItems.Add(new MenuItem("Map", new EventHandler(this.ShowMapDrive)));
                ToolsItem.MenuItems.Add(new MenuItem("-"));

                       

//This is the extra Sub Menu I am trying to add
                            var Setss = new MenuItem();
                             Setss.Name = xmlNode.SelectSingleNode("Name").InnerText;
                            Setss.Text = xmlNode.SelectSingleNode("Name").InnerText;
                            Setss.Click += Menu_Click;
                            ToolsItem.MenuItems.Add(0, Windset);
//To here





               
                this.notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { Spacer3, UpDateAsset, Spacer2, MenuMessage, Spacer4, MyComputer, Spacer, ToolsItem, Spacer, AdminItem });

Any advise would be great
Cheers
 
Solution
There's a lot wrong here.
I have written a Taskbar Windows Form
What does that mean? Are you saying, without actually saying, that you are using a NotifyIcon to display your app in the Windows notification area, AKA the system tray? If so, please say that. If not, please explain what you actually do mean.

It looks like you are using a ContextMenu, which has been obsolete since 2005. Not quite 20 years yet, so I guess that's not too bad. You should be using a ContextMenuStrip, to which you would add ToolStripMenuItems. Unless the items are changing at run time, you should be adding those items in the designer, not in code. If you were going to do it in code, here's a simple example...
There's a lot wrong here.
I have written a Taskbar Windows Form
What does that mean? Are you saying, without actually saying, that you are using a NotifyIcon to display your app in the Windows notification area, AKA the system tray? If so, please say that. If not, please explain what you actually do mean.

It looks like you are using a ContextMenu, which has been obsolete since 2005. Not quite 20 years yet, so I guess that's not too bad. You should be using a ContextMenuStrip, to which you would add ToolStripMenuItems. Unless the items are changing at run time, you should be adding those items in the designer, not in code. If you were going to do it in code, here's a simple example:
C#:
contextMenuStrip1.Items.Add("Item1", null, (s, ea) => MessageBox.Show("Item1"));
contextMenuStrip1.Items.Add("Item2", null, (s, ea) => MessageBox.Show("Item2"));

var item3 = (ToolStripMenuItem)contextMenuStrip1.Items.Add("Item3");

item3.DropDownItems.Add("Item3A", null, (s, ea) => MessageBox.Show("Item3A"));
item3.DropDownItems.Add("Item3B", null, (s, ea) => MessageBox.Show("Item3B"));
You would assign the menu itself to the ContextMenuStrip property of a NotifyIcon or any control, rather than the ContextMenu property.
 
Solution
Actually, I just realised that there's more code there than I saw in the first place. That makes the OP a bit clearer that I thought it was, so I apologise for that. The advice still stands.
 

Latest posts

Back
Top Bottom