Unable to set TIME OnEvent during a Button OnClick event

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
user5274
Junior Coder
Posts: 43
Joined: Tue Aug 04, 2020 9:35 am

Unable to set TIME OnEvent during a Button OnClick event

Post by user5274 » Mon Jun 07, 2021 5:04 pm

Hi again, as the title suggests i am Unable to set TIME OnEvent during a Button OnClick event, the code immediately exits the SRT after the OnEvent and the OnEvent is never set and fired at the selected time. The below code is arbitrary code for example only. Thank you.

If this behavior is intended could you please suggest a workaround to be able to allow to set a TIME OnEvent based on User input. Thank you.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  object MSButton1: tMSButton
    Left = 8
    Top = 8
    Width = 175
    Height = 25
    Caption = 'Click Me to set OnEvent TIME'
    TabOrder = 0
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,BUTTONCLICKED_SET_ONEVENT_TIME
AddDialogHandler>Dialog1,,OnClose,SET_THIS_NUMBER

Show>Dialog1


SRT>BUTTONCLICKED_SET_ONEVENT_TIME
  Hour>HH
  Min>MM
  Let>MM=MM+1
  Format>%.2d,MM,MM
  Let>HHMM=%HH%%MM%
  SetDialogProperty>Dialog1,MSButton1,Caption,HHMM
  SetDialogProperty>Dialog1,MSButton1,Enabled,False
  OnEvent>TIME,HHMM,,DO_THAT_TIME_EVENT
  //nothing beyond the OnEvent command runs  <--------------------------------
  //and the OnEvent is never fired
  MDL>Time event set
END>BUTTONCLICKED_SET_ONEVENT_TIME

SRT>DO_THAT_TIME_EVENT
  OnEvent>TIME,HHMM,,
  MDL>Time event cleared
  GoSub>SET_THIS_NUMBER
END>DO_THAT_TIME_EVENT

SRT>SET_THIS_NUMBER
  Let>THISNUMBER=1
END>SET_THIS_NUMBER


Let>THISNUMBER=0
Repeat>THISNUMBER
  
  Wait>1
  
Until>THISNUMBER=1

MDL>Script ended

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Unable to set TIME OnEvent during a Button OnClick event

Post by JRL » Mon Jun 07, 2021 9:49 pm

Your conclusion is correct. An OnEvent> fails to function if its invoked from a subroutine called by a dialog event.

The solution is to use a Custom OnEvent> and have the dialog event simply set the Custom OnEvent's Variable to True. Be sure to set the variable back to something other than True in the first line of the called subroutine to prevent a loop.


Code: Select all

OnEvent>Custom,TestVar,Var1,BUTTONCLICKED_SET_ONEVENT_TIME

SRT>TestVar

END>TestVar

SRT>SetVar1
  Let>Var1=True
END>SetVar1


Dialog>Dialog1
object Dialog1: TForm
  object MSButton1: tMSButton
    Left = 8
    Top = 8
    Width = 175
    Height = 25
    Caption = 'Click Me to set OnEvent TIME'
    TabOrder = 0
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSButton1,OnClick,SetVar1
AddDialogHandler>Dialog1,,OnClose,SET_THIS_NUMBER
Show>Dialog1
SRT>BUTTONCLICKED_SET_ONEVENT_TIME
  Let>Var1=False
  Hour>HH
  Min>MM
  Let>MM=MM+1
  Format>%.2d,MM,MM
  Let>HHMM=%HH%%MM%
  SetDialogProperty>Dialog1,MSButton1,Caption,HHMM
  SetDialogProperty>Dialog1,MSButton1,Enabled,False
  OnEvent>TIME,HHMM,,DO_THAT_TIME_EVENT
  //nothing beyond the OnEvent command runs  <--------------------------------
  //and the OnEvent is never fired
  MDL>Time event set
END>BUTTONCLICKED_SET_ONEVENT_TIME
SRT>DO_THAT_TIME_EVENT
  OnEvent>TIME,HHMM,,
  MDL>Time event cleared
  GoSub>SET_THIS_NUMBER
END>DO_THAT_TIME_EVENT
SRT>SET_THIS_NUMBER
  Let>THISNUMBER=1
END>SET_THIS_NUMBER
Let>THISNUMBER=0
Repeat>THISNUMBER
  
  Wait>1
  
Until>THISNUMBER=1

MDL>Script ended

user5274
Junior Coder
Posts: 43
Joined: Tue Aug 04, 2020 9:35 am

Re: Unable to set TIME OnEvent during a Button OnClick event

Post by user5274 » Wed Jun 09, 2021 12:58 pm

Thanks for the workaround, if this behavior is intended then I'll continue using this workaround.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts