By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

How to use MsgBox in AutoIt

The MsgBox is useful for testing your scripts, so lets quickly cover how to use this function.

Edited: 2015-03-16 23:29

AutoIt Logo

The AutoIt MsgBox function is used to display a small message box or dialog to the user. Lets start by showing how to create a message dialog box containing nothing but a OK button:

MsgBox(0, "My Title", "Click OK to Continiue")

Often you will get an error message like: "Incorrect number of parameters in function call", as shown below:

C:\Users\MyComputer\Desktop\MsgBox.au3 (1) : ==> Incorrect number of parameters in function call.:
MsgBox(0, "Test")

This often happens for beginners, who do not remember that the MsgBox function takes both a title and a text message after the flag parameter.

The flag parameter of the MsgBox

The buttons available to the user to push in the message box, are controlled by the flag parameter. The MsgBox is very simple, so if you want more control over they dialog, you should instead create your own GUI. For example, to include both a OK and a Cancel Button, you may use a flag value of 1 instead of 0.

MsgBox(1, "My Title", "Click OK to Continiue")

There are many flag values that you can use, you should look up the reference to learn more.

You can also combine flag values, to do that you may want to use the plus (+) sign to separate the flag values.

MsgBox(48+1, "Test", "Message Box with Icon")

Knowing which Button was Pressed

lets show how to react differently, depending on which button was pressed. To do that, we will be using a if statement to check the return value of the function, like demonstrated below:

$MyBox = MsgBox(1, "Test", "haha")

If $MyBox == 1 Then
 MsgBox(0,"Message Box Title","You Pressed the OK button!")
ElseIf $MyBox == 2 Then
 MsgBox(0,"Message Box Title","You Pressed Cancel!")
EndIf

As you can see, the way we got the return value, was to save it into a variable. The MsgBox will still be created, even when using it with a variable – many beginners don't realize this – it will be useful to know when working with many other functions of AutoIt as well.

Button Return Values

You can refer to this tutorial for other buttons as well. Below is a list of the different buttons, and their return value.

Button:Return Value:
OK1
CANCEL2
ABORT3
RETRY4
IGNORE5
YES6
NO7
TRY AGAIN **10
CONTINUE **11

See also

  1. How to make GUIs with AutoIt