Ben
Ben

Reputation: 99

How to let all items in multi-level PopupMenu act as one radiogroup?

I have a PopupMenu with submenus and only one item in total shall be checked at a time. GroupIndex and RadioItem properties do not work outside of the respective submenus as far as I have tried.

I have found this piece of code to check a PopupMenu and its direct sub-components but I haven't had any luck with creating a popup-wide variety of this. I need a solution that is fast - the PopupMenu has 4x14 entries, always iterating through all menus and subentries can't be the best solution for this, I suppose.

Is there a simple property for this that I am missing or is the rocky path of iteration my only option?

Upvotes: 3

Views: 1330

Answers (2)

NGLN
NGLN

Reputation: 43659

NGLN's answer is better, but if you really don't want or don't like to use an ActionList, then this routine will also do:

procedure CheckMenuItem(Item: TMenuItem);

  procedure UncheckMenu(Menu: TMenuItem; GroupIndex: Byte);
  var
    I: Integer;
  begin
    if Menu.RadioItem and (Menu.GroupIndex = GroupIndex) then
      Menu.Checked := False;
    for I := 0 to Menu.Count - 1 do
      UncheckMenu(Menu[I], GroupIndex);
  end;

begin
  if (not Item.Checked) and Item.RadioItem and (Item.GroupIndex <> 0) then
  begin
    UncheckMenu(Item.GetParentMenu.Items, Item.GroupIndex);
    Item.Checked := True;
  end;
end;

Upvotes: 2

NGLN
NGLN

Reputation: 43659

Add all 56 items as actions to one ActionList and give all GroupIndex properties the same value.

Now, add menu-items, sub-menu's and sub-sub-menu's in any tree-like fashion and link each of them to an action. Checking one menu-item, wherever positioned, will automatically uncheck all others.

Et voilà!

Upvotes: 8

Related Questions