Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add menu items separators programmatically on Windows?

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;
like image 668
ThN Avatar asked Mar 01 '26 14:03

ThN


1 Answers

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.

like image 128
David Heffernan Avatar answered Mar 04 '26 10:03

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!