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.
|TRY AGAIN **||10|