Mirror

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


Problem/Question/Abstract:

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?

Answer:

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);
var
  ix: integer;
  item2: TMenuItem;
begin
  for ix := 1 to item.Count - 1 do {ignore first}
  begin
    item2 := item.Items[ix];
    if ix mod maxrows = 0 then
      item2.Break := mbBreak
    else
      item2.Break := mbNone;
  end;
end;


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
  inc(count);
FormatMenu(PopupMenu.Items, count);

<< Back to main page