Home
What's New
Delphi 3.0 Tips
Questions
Development Tools
Links

Creating Menus at Run Time

Type
  TNewMenuItem = class (TMenuItem)
  public
    procedure Click; override;
  end;
end;

procedure TNewMenuItem.Click;
begin
  {TMenuItem's MenuIndex propoerty may be useful here.
   Its the position of this menu item in its
   menu (beginning at 0).
  }
end;

{A TCaption and string are actually the same type.
 The next function would change a menu caption of:
 1 First Menu Item
 to
 First Menu Item
}
function CapToStr (s : TCaption) : string;
begin
  if (s = '') or (s[1] <> '&') then begin
    result := s;
    exit;
  end;

  result := copy (s, 4, length (s) - 3);
end;

function StrToCap (s : string; i : byte): TCaption;
begin
  result := s;
  if i > 9 then exit;
  result := '&' + chr (i + 48) + #32 + result;
end;

procedure TForm1.AddNewMenuItem (cap : TCaption; i : byte);
var NewMenuItem : TNewMenuItem;
begin
  MenuItem := TNewMenuItem.Create (ExistingMenu);
  MenuItem.Caption := StrToCap (fName, i);

  if (ExistingMenu.Count mod MAX_MENU_ROWS = 0)
  and (ExistingMenu.Count > 0) then
    NewMenuItem.Break := mbBarBreak;

  ExistingMenu.Add (NewMenuItem);
  EnableView;
end;