Dynamically enable items in popup menu

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Dynamically enable items in popup menu

Postby GregReese » Thu May 28, 2015 7:40 am

Hi folks,
I'm trying to dynamically (at run time) enable/disable menu items in a popup menu but keep getting exceptions. I think I've tracked the problem down to the Items property of TPopupMenu, which is read-only and thus prevents me from changing any of the menu items it contains. Is there a way to enable/disable the menu items in a TPopupMenu at run-time? I have XE4.
Thanks.
Greg Reese
GregReese
 
Posts: 2
Joined: Thu May 28, 2015 7:31 am

Re: Dynamically enable items in popup menu

Postby HsiaLin » Thu May 28, 2015 10:47 am

MenuItemName1->Enabled = true;
MenuItemName1->Enabled = false;
HsiaLin
BCBJ Master
BCBJ Master
 
Posts: 299
Joined: Sun Jul 08, 2007 6:29 pm

Re: Dynamically enable items in popup menu

Postby minas » Thu May 28, 2015 1:24 pm

HsiaLin wrote:MenuItemName1->Enabled = true;
MenuItemName1->Enabled = false;


or
/*e.g for the first MenuItem -index 0 */
PopupMenu1->Items->Items[0]->Enabled= true;
PopupMenu1->Items->Items[0]->Enabled= false;
-
User avatar
minas
BCBJ Guru
BCBJ Guru
 
Posts: 196
Joined: Sat Jul 10, 2004 6:09 am
Location: Greece

Re: Dynamically enable items in popup menu

Postby rlebeau » Thu May 28, 2015 2:29 pm

GregReese wrote:I'm trying to dynamically (at run time) enable/disable menu items in a popup menu but keep getting exceptions.


Such as? What does your enable/disable code actually look like?

GregReese wrote:I think I've tracked the problem down to the Items property of TPopupMenu, which is read-only and thus prevents me from changing any of the menu items it contains.


The TPopupMenu::Items property itself is read-only, but that only means that you cannot assign a new collection of menu items to the TPopupMenu:

Code: Select all
PopupMenu1->Items = NewItems; // <-- compiler error
PopupMenu1->Items->Assign(NewItems); // <-- runtime error


But the content of the TPopupMenu::Items collection is certainly not read-only. You can access and modify individual TMenuItems objects within the TPopupMenu as needed.

GregReese wrote:Is there a way to enable/disable the menu items in a TPopupMenu at run-time?


Use the TMenuItem::Enabled property of the individual menu items, like HsiaLin and minas showed you.

An alternative is to assign TAction objects to the relevant menu items, and then you can use the TAction::OnUpdate or TActionManager::OnUpdate events to enable/disable the TAction objects as needed, thus enabling/disabling the menu items that they are attached to.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1560
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Dynamically enable items in popup menu

Postby GregReese » Fri May 29, 2015 8:43 am

Hi All,

I was trying to do what minas suggested, i.e.,

Code: Select all
/*e.g for the first MenuItem -index 0 */
PopupMenu1->Items->Items[0]->Enabled= true;
PopupMenu1->Items->Items[0]->Enabled= false;


Instead, I had

Code: Select all
PopupMenu1->Items[0].Enabled= true;
PopupMenu1->Items[0].Enabled= false;

which caused the runtime error. My bad - minas's code works.

Thanks for everyone's help.

Greg Reese
GregReese
 
Posts: 2
Joined: Thu May 28, 2015 7:31 am


Return to Technical

Who is online

Users browsing this forum: No registered users and 16 guests