Button On Dialog

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
alexmuir
Newbie
Posts: 6
Joined: Wed Jul 22, 2009 10:16 pm

Button On Dialog

Post by alexmuir » Tue Aug 07, 2018 5:34 pm

Hello
Sorry please bear with me but starting down the MS road.
I have created a dialogue
I have place a button the dialogue.

How do I get the button clicked to run another MS macro?
Thought it might be Action in the Properties but alas no.

Thanks for your patience.

Alex

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Button On Dialog

Post by Marcus Tettmar » Tue Aug 07, 2018 9:30 pm

You need to use AddDialogHandler and set up an OnClick event.

Have a look at the Calculator sample macro which is in your Samples folder within Macro Scheduler.

Also see AddDialogHandler in the help file.

Also see:
https://help.mjtnet.com/article/40-an-i ... dal-dialog
And:
https://help.mjtnet.com/article/41-an-i ... t-handlers
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

alexmuir
Newbie
Posts: 6
Joined: Wed Jul 22, 2009 10:16 pm

Re: Button On Dialog

Post by alexmuir » Wed Aug 08, 2018 5:58 am

Marcus
Thanks for your prompt response.
I think I am nearly there?

Where am I going wrong?

//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1

Dialog>Dialog1
object Dialog1: TForm
Left = 715
Top = 325
HelpContext = 5000
BorderIcons = [biSystemMenu]
Caption = 'QUICK RUNS'
ClientHeight = 211
ClientWidth = 476
Color = 14938520
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
ShowHint = True
OnTaskBar = False
PixelsPerInch = 96
TextHeight = 13
object MSButton1: tMSButton
Left = 69
Top = 41
Width = 75
Height = 25
Caption = 'Hardware'
Font.Charset = ANSI_CHARSET
Font.Color = clPurple
Font.Height = -13
Font.Name = 'Univers 55'
Font.Style = [fsBold]
ParentFont = False
TabOrder = 0
DoBrowse = False
BrowseStyle = fbOpen
end
end
EndDialog>Dialog1

show Dialog1,modalResult
AddDialogHandler>Dialog1,MSButton1,Onclick,DoClick


SRT>DoClick
MessageModal>MSButton1,Run>notepad.exe
End>DoClick

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Button On Dialog

Post by Marcus Tettmar » Wed Aug 08, 2018 6:19 am

Ok let's run through your code. We can ignore the dialog definition. So let's look at this:

show Dialog1,modalResult
AddDialogHandler>Dialog1,MSButton1,Onclick,DoClick

SRT>DoClick
MessageModal>MSButton1,Run>notepad.exe
End>DoClick

Ok, so, ignoring the syntax errors, the first line shows the dialog and waits for the user to close it. The script won't continue until the dialog is closed. So we show the dialog, the user closes it and then script moves to the next line, which is AddDialogHandler. So NOW we set up a dialog handler. But the dialog has already closed. So it will never run. Next we have a subroutine, which is never called. And then the script ends. So nothing happens.

Ok, so first mistake is that the AddDialogHandler is being set up AFTER the dialog has closed. So let's move that around:

Code: Select all

AddDialogHandler>Dialog1,MSButton1,Onclick,DoClick
Show>Dialog1,modalResult
I've also add the chevron for you.

Now, let's look at the subroutine. What does this line do:

MessageModal>MSButton1,Run>notepad.exe

It's going to show a message box with this text:

"MSButton1,Run>notepad.exe"

Run>notepad.exe would be a command in it's own right and would run notepad. So perhaps you meant that on it's own line. So let's change the subroutine to:

Code: Select all

SRT>DoClick
  MessageModal>MSButton1
  Run>notepad.exe
End>DoClick
So working code, without the dialog definition, would be:

Code: Select all

AddDialogHandler>Dialog1,MSButton1,Onclick,DoClick
Show>Dialog1,modalResult

SRT>DoClick
  MessageModal>MSButton1
  Run>notepad.exe
End>DoClick
However, since you are showing a modal dialog (one that halts the script until it is closed) and not doing anything else why not just set the modal result of the button and act on that:

Code: Select all

Show>Dialog1,modalResult
If>modalResult=<whatever you set the modal result of the button to in dialog properties>
  Run>notepad.exe
Endif
With a modal dialog and modal result, you don't need a dialog handler, because the script waits until the dialog is closed and can then see what value was returned in modal result. In the dialog editor, select the button and then find the modal result property and set a value there. Note that when you close the dialog via the close button in the title bar (X) or similar, modal result will be set to 2, so avoid using 2 for your button. It also means you can check your returned value and if it's 2 you can assume the user CANCELLED (i.e. closed without clicking your button).

See:
https://www.mjtnet.com/manual/show.htm

Also:
https://www.mjtnet.com/manual/modal_vs_ ... ialogs.htm
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

alexmuir
Newbie
Posts: 6
Joined: Wed Jul 22, 2009 10:16 pm

Re: Button On Dialog

Post by alexmuir » Thu Aug 09, 2018 6:55 pm

Hello Marcus

I assumed that I could call any file
EndDialog>Dialog1
AddDialogHandler>Dialog1,MSButton1,Onclick,DoClick
Show>Dialog1,modalResult
SRT>DoClick
MessageModal>MSButton1
Run>C:\Office\CONFIDENTIALITYUNDERTAKING.docx
End>DoClick

but now I get an error message saying

0 Error in : Diag
Line 47 - Error Executing
C:\0fjice\CONFIDENTIALITYUNDERTAK|NG.docx
(19320). On Win64 if trying to run a 64 bit system
process try bypassing file system redirection with
RP_W|N64PROCESS or referring to SYS_NAT|

What am I doing wrong?

Many thanks

Alex

alexmuir
Newbie
Posts: 6
Joined: Wed Jul 22, 2009 10:16 pm

Re: Button On Dialog

Post by alexmuir » Thu Aug 09, 2018 7:26 pm

Marcus
Fixed. worked it out
Used ExecuteFile

Alex

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