How to split up a TPopupMenu into columns at runtime (Views: 100)


I have a PopupMenu, which contains a lot of menu items. Sometimes the user can't see all menus on the screen. The same problem occurs with MainMenu. How I can split PopupMenu at runtime that it will be all visible on the screen?


TMenuItem has a Break property which you can use to split a menu up into columns. The following procedure lets you specify the number of items you want in each column:

procedure FormatMenu(item: TMenuItem; maxrows: integer);
  ix: integer;
  item2: TMenuItem;
  for ix := 1 to item.Count - 1 do {ignore first}
    item2 := item.Items[ix];
    if ix mod maxrows = 0 then
      item2.Break := mbBreak
      item2.Break := mbNone;

You call it like:

FormatMenu(PopupMenu.Items, 4);

If instead of an absolute number, you want a specific number of columns, say three:

count := PopupMenu.Items.Count div 3;
if PopupMenu.Items.Count mod 3 > 0 then
FormatMenu(PopupMenu.Items, count);

<< Back to main page