Method

# MessageBox

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Displays message box showing the `String</api/data_types/string>` passed. With the exception of simple message boxes, you should use the `MessageDialog</api/user_interface/desktop/messagedialog>`, `WebDialog</api/user_interface/web/webdialog>` or `MobileMessageBox</api/user_interface/mobile/mobilemessagebox>` classes instead.

## Usage

When used in **Web** apps:

``` xojo
MessageBox(message)
```

Calling <span class="title-ref">MessageBox</span> in a session displays the dialog box. Unlike with desktop, this call is not synchronous.

Calling <span class="title-ref">MessageBox</span> outside a session depends on whether you are running from a built app or from the IDE:

- Running in a built app: It prints the message to the console just like a console app.
- Running from the IDE: It displays the message in the IDE's message pane.

When used in **Mobile** apps:

``` xojo
MobileMessageBox.Message = message
MobileMessageBox.Show
```

Displays *message* in a browser dialog with a single OK button.

When used in **Desktop** apps:

``` xojo
MessageBox(message)
```

| Part    | Type                             | Description                                            |
|---------|----------------------------------|--------------------------------------------------------|
| message | `String</api/data_types/string>` | Any valid `string</api/data_types/string>` expression. |

<div class="tip">

<div class="title">

Tip

</div>

In desktop and web apps, if the message contains two `EndOfLine</api/text/endofline>` characters back-to-back, the text that precedes them will be displayed larger, bold font style while the text that follows will be displayed in a smaller, plain font style.

</div>

## Notes

<div class="warning">

<div class="title">

Warning

</div>

You should avoid using <span class="title-ref">MessageBox</span> for displaying debugging messages. The displaying of the Message Box will alter event order and may give unexpected results. Instead use the `Debugger</getting_started/debugging/debugger_usage>`, `System.DebugLog<system.debuglog>` or your own logging mechanism.

</div>

### Using MessageBox in desktop projects

A message can be presented to the user with either the <span class="title-ref">MessageBox</span> method or the `MessageDialog</api/user_interface/desktop/messagedialog>` class. The <span class="title-ref">MessageBox</span> method is recommended for simple informational messages only. For other situations, the `MessageDialog</api/user_interface/desktop/messagedialog>` class is more appropriate. It enables you to add up to three buttons, label them in any way, and take any action after the user clicked a button.

The Message box opened by <span class="title-ref">MessageBox</span> has a Title bar. On Windows, the dialog also has a Close widget in its Title bar. The dialog can be closed either by clicking OK or clicking the Close widget. On Windows and Linux, the width increases to accommodate the longest paragraph. On macOS, the Message box has a fixed width and the text word-wraps to fit the width of the <span class="title-ref">MessageBox</span>.

Multiple paragraphs can be passed in the message parameter by separating each paragraph with the `EndOfLine</api/text/endofline>` function.

Closing a <span class="title-ref">MessageBox</span> gives the focus back to the window, which calls its Activated event.

On macOS only the Caution icon is displayed. This is the result of changes made by Apple in HID specs. See [here](http://developer.apple.com/legacy/mac/library/qa/qa2004/qa1378.html) for details.

### Using MessageBox in web projects

On the web, <span class="title-ref">MessageBox</span> is asynchronous which means that the code won't stop when its called but will instead continue. This can cause problems if, for example, you are calling `SetFocus<webuicontrol.setfocus>` to put the focus back into a `WebTextField</api/user_interface/web/webtextfield>` after the <span class="title-ref">MessageBox</span> is displayed. See the **Using MessageBox to Inform the User** web project example for details on how to solve this.

## Sample code

This code displays a one line Message box.

``` xojo
MessageBox("Hello, world!")
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following desktop or web code displays a multiline Message box. The text after the two `EndOfLine</api/text/endofline>` function calls appears in plain type with a smaller font size. Please note that it is entered as one line in the Code Editor.

``` xojo
MessageBox("Please enter all your credit card info, bank accounts, and trust funds before proceeding to run my shareware application." _
  + EndOfLine + EndOfLine + "Any additional voluntary contributions are gratefully accepted.")
```

## Compatibility

|                       |                      |
|-----------------------|----------------------|
| **Project Types**     | Desktop, Mobile, Web |
| **Operating Systems** | All                  |

<div class="seealso">

`EndOfLine</api/text/endofline>`, `MessageDialog</api/user_interface/desktop/messagedialog>`, `WebDialog</api/user_interface/web/webdialog>`, `MobileMessageBox</api/user_interface/mobile/mobilemessagebox>` classes.

</div>
