Add control button to MsgBox

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Add control button to MsgBox

22 Jul 2017, 19:39

Hello,

Is there a way to add a control button to a MsgBox which requires clicking for the script to continue?

For example, suppose you have a script which after several steps, displays a MsgBox and concurrently, pauses the script. Can the MsgBox include a "Continue" button which the user must click for the script to continue?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Add control button to MsgBox

22 Jul 2017, 20:28

In a way, yes. You can use ControlSetText to change the text of the message box buttons respectively to how they appear in the window. For example:
in a message box that has the option: 4, the Message box will have "Yes" and "No" buttons, the "Yes" button will be Button1, and the "No" button will be Button2.
I can give you a function to set the text of the buttons in respective order;

Code: Select all

MsgBox(Message := "Press Ok to Continue.", Title := "", Type := 0, B1 := "", B2 := "", B3 := "", Time := "") {
	If (Title = "")
		Title := A_ScriptName
	If (B1 != "") || (B2 != "") || (B3 != "")
		SetTimer, ChangeButtonNames, -10
	MsgBox, % Type, % Title, % Message, % Time
	Return

	ChangeButtonNames:
		IfWinNotExist, %Title%
			Return
		WinActivate, % Title
		ControlSetText, Button1, % (B1 = "") ? "Ok" : B1, % Title
		Try ControlSetText, Button2, % (B2 = "") ? "Cancel" : B2, % Title
		Try ControlSetText, Button3, % (B3 = "") ? "Close" : B3, % Title
	Return
}
Just Specify the following in a script anywhere you want the Message box to appear and it will show with custom buttons:

Code: Select all

MsgBox("Press ""Continue"" to Continue.",, 0, "Continue")
Hope this helps.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Add control button to MsgBox

22 Jul 2017, 20:35

Hi.
Yes you can, but only with predefined buttons that the MsgBox offers.
The help file says
To determine which button the user pressed in the most recent MsgBox, use the IfMsgBox command. For example:
MsgBox, 4,, Would you like to continue? (press Yes or No)
IfMsgBox Yes
MsgBox You pressed Yes.
else
MsgBox You pressed No.
You can write code like this:

Code: Select all

...
MsgBox, 4,, Would you like to abort?
IfMsgBox Yes
    return
    ...
Now, if you click no, than the return command will be skipped and the code after will be processed.
Einfach nur ein toller Typ. :mrgreen:
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Add control button to MsgBox

22 Jul 2017, 21:23

Thanks a bunch for the responses.
Delta Pythagorean wrote:I can give you a function to set the text of the buttons in respective order;

Code: Select all

MsgBox(Message := "Press Ok to Continue.", Title := "", Type := 0, B1 := "", B2 := "", B3 := "", Time := "") {
	If (Title = "")
		Title := A_ScriptName
	If (B1 != "") || (B2 != "") || (B3 != "")
		SetTimer, ChangeButtonNames, -10
	MsgBox, % Type, % Title, % Message, % Time
	Return

	ChangeButtonNames:
		IfWinNotExist, %Title%
			Return
		WinActivate, % Title
		ControlSetText, Button1, % (B1 = "") ? "Ok" : B1, % Title
		Try ControlSetText, Button2, % (B2 = "") ? "Cancel" : B2, % Title
		Try ControlSetText, Button3, % (B3 = "") ? "Close" : B3, % Title
	Return
}
Just Specify the following in a script anywhere you want the Message box to appear and it will show with custom buttons:

Code: Select all

MsgBox("Press ""Continue"" to Continue.",, 0, "Continue")
Sorry, but can you provide a bit more explanation re how to use this?

I'm pretty sure I understand your statement preceding the second (one-line) code box (ie Just Specify the following in a script anywhere...) — and I did get the MsgBox to appear — but I'm not sure what to do with the previous larger box of code. For example, suppose the following represents some script:

Code: Select all

F1::
Step A
Step B
Step C
Step D
Return
and suppose you want to pause and bring up the MsgBox after Step A and get a user response before the script continues with Steps B, C & D. Then I presume, the one-line code is inserted like this:

Code: Select all

F1::
Step A
MsgBox("Press ""Continue"" to Continue.",, 0, "Continue")
Step B
Step C
Step D
Return
Is that correct?

And if so, should your longer code be inserted before the script (ie above "F1::"), after the script (ie below "Return") or as a separate stand-alone script?

Or am I deep in left field picking dandelions (vs picking something else) and the correct answer is altogether different?

Thanks again
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Add control button to MsgBox

22 Jul 2017, 21:31

WeThotUWasAToad wrote: And if so, should your longer code be inserted before the script (ie above "F1::"), after the script (ie below "Return") or as a separate stand-alone script?
The "longer code," can be put at the beginning or end of the script. The "longer code" is simply a function, functions can be put anywhere as long as it isn't mixed between another function or a label.
So, in short, put it after the return in the hotkey it will work completely fine.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Add control button to MsgBox

22 Jul 2017, 21:35

Hi.
You choosed a very difficult example.
If it is not necessary, that the word continue is written on your button in the MsgBox make it easy and take this example:

Code: Select all

F1::
Step A
MsgBox, 4,, Would you like to abort?
	IfMsgBox Yes
	return
Step B
Step C
Step D
Return
Now, when you press No, your script will continue. Otherwise, you script will stop.
Einfach nur ein toller Typ. :mrgreen:
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Add control button to MsgBox

22 Jul 2017, 21:37

[quote="divanebaba"]Yes you can, but only with predefined buttons that the MsgBox offers.[quote]

OK, I got this one to work:

Code: Select all

F1::
Send {Right 9}
	Sleep, 500
MsgBox, 4,, Would you like to abort?
IfMsgBox Yes
	ExitApp
Else
Send {Down 9}
Return
It looks like I need to go read up on MsgBox's again because I did not realize it had those options.

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Add control button to MsgBox

22 Jul 2017, 21:48

Delta Pythagorean wrote:
WeThotUWasAToad wrote: And if so, should your longer code be inserted before the script (ie above "F1::"), after the script (ie below "Return") or as a separate stand-alone script?
The "longer code," can be put at the beginning or end of the script. The "longer code" is simply a function, functions can be put anywhere as long as it isn't mixed between another function or a label.
So, in short, put it after the return in the hotkey it will work completely fine.
Thanks for the clarification. It works like a charm:

Code: Select all

F1::
Send {Right 9}
	Sleep, 500
MsgBox("Press ""Continue"" to Continue.",, 0, "Continue")
;IfMsgBox Yes
;	ExitApp
;Else
Send {Down 9}
Return

MsgBox(Message := "Press Ok to Continue.", Title := "", Type := 0, B1 := "", B2 := "", B3 := "", Time := "") {
	If (Title = "")
		Title := A_ScriptName
	If (B1 != "") || (B2 != "") || (B3 != "")
		SetTimer, ChangeButtonNames, -10
	MsgBox, % Type, % Title, % Message, % Time
	Return

	ChangeButtonNames:
		IfWinNotExist, %Title%
			Return
		WinActivate, % Title
		ControlSetText, Button1, % (B1 = "") ? "Ok" : B1, % Title
		Try ControlSetText, Button2, % (B2 = "") ? "Cancel" : B2, % Title
		Try ControlSetText, Button3, % (B3 = "") ? "Close" : B3, % Title
	Return
}
I think the problem earlier was that I tried inserting it in a much more complex script and it must have been causing some type of conflict. However, when I tried it in the simple little {Right} & {Down} script above, it works great.

It's great to see different approaches to the same question. :)

Thanks a bunch to both of you for your assistance!
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, doanmvu and 297 guests