How to add a separator to a WinForms ContextMenu?

前端 未结 8 1427
暖寄归人
暖寄归人 2020-12-29 00:47

Inside my control, I have:

ContextMenu = new ContextMenu();
ContextMenu.MenuItems.Add(new MenuItem(\"&Add Item\", onAddSpeaker));
ContextMenu.MenuItems.A         


        
相关标签:
8条回答
  • 2020-12-29 01:25

    Horizontal separators are cool, but what if you want a vertical separator instead?

    Well, worry ye not - you can have one!

    Set BarBreak property to true on the MenuItem which should be the first one after the seperator:

    var item = new MenuItem(text: "Settings", onClick: SomeFunction) { BarBreak = true };
    

    To add the item to a MenuItems collection: yourContextMenu.MenuItems.Add(item).

    0 讨论(0)
  • 2020-12-29 01:28

    ContextMenu has a constructor which receives an array of MenuItem objects. Needless to say, you can't add a string to that array. You can however get a seperator by adding a new MenuItem("-"):

        var contextMenu = new ContextMenu(new[]
        {
            timerMenuItem,
            keypressMenuItem,
            new MenuItem("-"), // Seperator
            new MenuItem(text: "Exit", onClick: (sender, args) => Application.Exit())
        });
    
    0 讨论(0)
提交回复
热议问题