Recent

Author Topic: Enabling/disabling program functions  (Read 2308 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
Enabling/disabling program functions
« on: September 27, 2021, 03:30:09 pm »
Hi,

I have an application where I do want to enable/disable several functions (e.g. main menu items and so on) based on the state of the program (which can get quite complicated). So an easy way could be to setup a timer which checks all circumstances twice a second or so which then enables/disables the functions - but that seems like quite a sick method for me.

Is there a better way to achieve that?

Thx,
--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

glorfin

  • Full Member
  • ***
  • Posts: 140
  • LMath supporter
Re: Enabling/disabling program functions
« Reply #1 on: September 27, 2021, 03:38:34 pm »
Hi! If this is GUI application made with Lazarus, you can use Application.OnIdle method. Consider also using TAction and its successors, it has a special method OnUpdate which is called from Application.Idle. Additional advantage is that you can bind same Action to menu items, hot keys and speedbutttons, so, their state is updated in a coordinated way.
« Last Edit: September 27, 2021, 03:40:19 pm by glorfin »

MarkMLl

  • Hero Member
  • *****
  • Posts: 3251
Re: Enabling/disabling program functions
« Reply #2 on: September 27, 2021, 03:42:06 pm »
If this is in the context of Lazarus/LCL, you can normally hook an event before a menu is displayed which allows you to enable/disable/hide as appropriate.

If you need even more control, you can at a pinch simulate a "main menu" using dropdowns. That can be handy if you also want e.g. a progress bar, or if you want to e.g. offset the help menu to the extreme RH end.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 739
Re: Enabling/disabling program functions
« Reply #3 on: September 27, 2021, 03:42:37 pm »
This sounds a lot like "Validation during input/run"

Define the go/no go criterias for each item that has to be enabled/disabled, and then use the proper Event-Handlers of the controls which should control the behavior
(Not sure if i've explained it well).

a "classic" is "Disable Login-Button as long as username and password are NOT provided"
In that case you would check the OnChange-Event of both TEdits checking if the TEdit is empty or not
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Bart

  • Hero Member
  • *****
  • Posts: 4472
    • Bart en Mariska's Webstek
Re: Enabling/disabling program functions
« Reply #4 on: September 27, 2021, 05:51:20 pm »
Hi! If this is GUI application made with Lazarus, you can use Application.OnIdle method.

That will cause a lot of flickering weh you disable/enable GUI components like a menu, better use the suggestion of MarkMLl for menu's.

Bart

glorfin

  • Full Member
  • ***
  • Posts: 140
  • LMath supporter
Re: Enabling/disabling program functions
« Reply #5 on: September 28, 2021, 04:12:30 pm »
Hi! If this is GUI application made with Lazarus, you can use Application.OnIdle method.

That will cause a lot of flickering weh you disable/enable GUI components like a menu, better use the suggestion of MarkMLl for menu's.

Bart
Why? It's a standard solution. Well, usually you do not even need to do it directly. Just define Action.OnUpdate, and it is automatically called from OnIdle. But anyway, why would you expect flickering?

Bart

  • Hero Member
  • *****
  • Posts: 4472
    • Bart en Mariska's Webstek
Re: Enabling/disabling program functions
« Reply #6 on: September 28, 2021, 06:31:47 pm »
I don't expect that, I saw it.
I used OnIdle to update my menu Items, and that caused a visual mess.
Now I update my menuItems upon opening.

Bart

glorfin

  • Full Member
  • ***
  • Posts: 140
  • LMath supporter
Re: Enabling/disabling program functions
« Reply #7 on: September 29, 2021, 06:54:18 pm »
Maybe depends on use of Invalidate/Repain etc. As I have said, I am using TAction.Enabled, and when its status is changed, LCL itself redraws all controls, linked to this Action, from Application.Idle, and there is no flickering.

 

TinyPortal © 2005-2018