How to set enable state of TMenuItem when it is shown lazarus / 31-LCL ---------------------------------------------------------------------- Muso · 2022-06-13 14:56 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 · 2022-06-13 15:25 Quote from: Muso on June 13, 2022, 04:56:05 pmOn 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 · 2022-06-13 15:35 Quote from: dseligo on June 13, 2022, 05:25:30 pmI 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 · 2022-06-13 15:37 Quote from: Muso on June 13, 2022, 05:35:19 pmQuote from: dseligo on June 13, 2022, 05:25:30 pmI 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 · 2022-06-13 15:38 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 · 2022-06-13 16:04 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 · 2022-06-13 16:08 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. ---------------------------------------------------------------------- Muso · 2022-06-13 16:12 Quote from: Handoko on June 13, 2022, 06:08:07 pmYou can make it more clearer if you provide the source code you already have. Well, it is just that whenever Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2022-06-13 16:28 Quote from: Muso on June 13, 2022, 05:38:55 pmuse 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Action1Update(Sender: TObject);begin  Action1.enabled := not MainForm.OverallTimer.Enabled;end; 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 · 2022-07-11 15:08 Quote from: Muso on June 13, 2022, 04:56:05 pmI 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.