How to use MsgBox in AutoIt

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

10699 views

Edited: 2017-12-19 17:20

AutoIt Logo

The AutoIt MsgBox function is used to display a small message box or dialog box to the user, which also allows us to accept user input.

MsgBox can also be very useful for testing parts of your script while you are still coding. Often you might want to know the value of a variable, or output of a function, and in such cases, it will be very handy.

Below is an example of how to create a simple dialog box containing an 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", slimier to the one shown below.

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

This happens because MsgBox needs 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

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials