How to add menu items separators programmatically on Windows?

3.2k views Asked by At

I have a mainmenu on my Form and I want to be able to insert separator to it programmatically not during design time. I went through context popup menu for the mainmenu that lists all the available properties and did not find anything that will allow me to insert separator. Google wasn't much of a help. So, how is this done in Delphi on Windows? I am using Delphi 2010.

I simply want to do something like the following but AddSeparator command doesn't exist:

MainMenu1.Items[5].AddSeparator;
1

There are 1 answers

0
David Heffernan On BEST ANSWER

Create a new menu item and set its caption to '-'.

var
  MenuItem: TMenuItem;
....
MenuItem := TMenuItem.Create(Menu); // Menu is the menu into which you are adding
MenuItem.Caption := '-';
Menu.Items.Add(MenuItem);

Instead of Add, which adds to the end of the menu, you can use Insert to insert the item into the middle of the menu.

The documentation says:

Specify a hyphen character (-) as the value of Caption for the menu item to indicate that the menu item is a separator.