How to add a separator to a WinForms ContextMenu?

前端 未结 8 1509
暖寄归人
暖寄归人 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: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())
        });
    

提交回复
热议问题