Recent

Author Topic: How to set enable state of TMenuItem when it is shown  (Read 798 times)

Muso

  • Sr. Member
  • ****
  • Posts: 356
How to set enable state of TMenuItem when it is shown
« on: June 13, 2022, 04:56:05 pm »
I wrote a quite complex program controlling several sensors etc. Therefore I have a lot of different states and depending on the state the program is in, some menu entries must be disabled.
I have now a lot of code just handling the enable state of the menus. However, I hope there is a more simple solution. My question is if this is possible:

On the OnDrawItem event I check the state my program is in  and depending on the results, the item gets en/disabled.

I tried but end up in an infinite loop of drawing since disable the menu triggers another redraw.
Has anybody an idea how to achieve what I want?

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: How to set enable state of TMenuItem when it is shown
« Reply #1 on: June 13, 2022, 05:25:30 pm »
On the OnDrawItem event I check the state my program is in  and depending on the results, the item gets en/disabled.

I would change the logic: enable/disable items somewhere else (e.g. where state changes), don't do it in OnDrawItem.

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to set enable state of TMenuItem when it is shown
« Reply #2 on: June 13, 2022, 05:35:19 pm »
I would change the logic: enable/disable items somewhere else (e.g. where state changes)
This is what I do. But I have dozens (sic!) exception routines and they get bigger and bigger since when e.g. a connection to one of the sensors is lost, an exception is triggered and I must en/disable the different menus accordingly. Since every exception leads to a different set of en/disabled menus, I cannot just sort this out to a separate routine.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: How to set enable state of TMenuItem when it is shown
« Reply #3 on: June 13, 2022, 05:37:32 pm »
I would change the logic: enable/disable items somewhere else (e.g. where state changes)
This is what I do. But I have dozens (sic!) exception routines and they get bigger and bigger since when e.g. a connection to one of the sensors is lost, an exception is triggered and I must en/disable the different menus accordingly. Since every exception leads to a different set of en/disabled menus, I cannot just sort this out to a separate routine.
use actions and actionlist. One action can be linked to multiple controls and it has an onUpdate event where you can check your states and en/disable as needed for the action.

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to set enable state of TMenuItem when it is shown
« Reply #4 on: June 13, 2022, 05:38:55 pm »
use actions and actionlist. One action can be linked to multiple controls and it has an onUpdate event where you can check your states and en/disable as needed for the action.

Sounds good. Do you have a pointer/website where I can learn more about this?

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to set enable state of TMenuItem when it is shown
« Reply #5 on: June 13, 2022, 06:04:21 pm »
Hmm, maybe I should be more clear what I need:

- I have e.g. a timer and whenever the timer is enabled, 5 menus have to be enabled, 5 other menus disabled

So I need a way to trigger the en/disabling whenever the timer is running or not.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: How to set enable state of TMenuItem when it is shown
« Reply #6 on: June 13, 2022, 06:08:07 pm »
You can make it more clearer if you provide the source code you already have.

What you want to achieve can be done using several ways. So if you provide the code, we can show you which one is the best for your case.
« Last Edit: June 13, 2022, 06:10:12 pm by Handoko »

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to set enable state of TMenuItem when it is shown
« Reply #7 on: June 13, 2022, 06:12:00 pm »
You can make it more clearer if you provide the source code you already have.
Well, it is just that whenever

Code: Pascal  [Select][+][-]
  1. MainForm.OverallTimer.Enabled:= true;
menus must be disabled.

So it is a general thing because I also need this for other objects, not only timers. So I need a way that when I set a property of an objects an action is triggered.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: How to set enable state of TMenuItem when it is shown
« Reply #8 on: June 13, 2022, 06:28:05 pm »
use actions and actionlist. One action can be linked to multiple controls and it has an onUpdate event where you can check your states and en/disable as needed for the action.

Sounds good. Do you have a pointer/website where I can learn more about this?
here are some generic steps.
Start by dropping an actionlist on your form with the menus.
Double click to open the editor and add a generic action.
Select a menu that you will convert to use the action and move the onclick event of the menu to the onclick event of the action use copy paste.
Double click the OnUpdate event of the action and write some code in the form of
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Action1Update(Sender: TObject);
  2. begin
  3.   Action1.enabled := not MainForm.OverallTimer.Enabled;
  4. end;
  5.  
set the action's caption property the same as the menu's caption.
Select your menu item and set the action property to action1.

that should get you started using actions.

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to set enable state of TMenuItem when it is shown
« Reply #9 on: July 11, 2022, 05:08:40 pm »
I have now a lot of code just handling the enable state of the menus. However, I hope there is a more simple solution.
I found now a suitable solution. That is kind of a hack, but works fine for me:

I added a checkbox to the form and made it invisible. I use this as global Boolean variable. In its OnChange event I can set the different enable states of the menus and other form elements.

 

TinyPortal © 2005-2018