Files
docs-desktop/dotnet-desktop-guide/net/wpf/windows/how-to-open-message-box.md
T
Andy (Steve) De GeorgeandGenevieve Warren ea8529f082 WPF publish initial content (#1032)
* Add overview article for new WPF (#178)

* Remove previous WPF .NET 5 content

* Overview article

* New article: Create new WPF project (#183)

* Basic index file

* Finish article for new project.

* acro

* markdown fix

* Fix build errors

* fix code lang

* fix code lang

* Add differences article for WPF (#186)

* Fix VS version for create app

* Add differences article

* Minor

* fix overview styles code

* Add code langs for overview

* Port Window Overview WPF article (#1011)

* Add Window overview article

* Add TOC

* Remove temp code

* Fix headers

* Port final Windows related articles (#1015)

* Initial test commit

* Add system dialogs

* 75% complete

* Add images

* Fix linter

* Add to toc

* Add more howto

* Fix code

* Add get/set main window

* Add missing code

* Add to toc

* Fix warnings

* Fix warnings

* missing code

* Update see also

* fix xref

* Migrate wpf net core controls-styles articles (#1023)

* Migrate control-styles

* Fix links

* Fix links

* Port databinding article. (#1022)

* Migrated databinding overview

* update toc

* Fix links/snippets

* fix links

* Move to correct folder

* Port initial resources docs (#1026)

* initial resources docs

* Build errors

* Update dotnet-desktop-guide/net/wpf/systems/xaml-resources-overview.md

Co-authored-by: Genevieve Warren <[email protected]>

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* Feedback

* Add app article

* Add system article

* Minor

* meta

* fix toc

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

Co-authored-by: Genevieve Warren <[email protected]>

* Add xaml article

* reformat redirect

* Redirects

* WPF update XAML article  (#1031)

* convert snippets

* minor edits

* minor

* Fix warnings

* Fixes #1028

* Fixes #1028

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* update redirects

Co-authored-by: Genevieve Warren <[email protected]>

* minor updates to link

* Readd migration article

* Warning fixes

* Fix lint

* Minor updates

* Fix warnings for TOC/YML

* last warnings

Co-authored-by: Genevieve Warren <[email protected]>
2021-04-15 12:52:05 -07:00

5.8 KiB

title, description, ms.date, dev_langs, helpviewer_keywords
title description ms.date dev_langs helpviewer_keywords
How to display a message box Learn about how to show a message box in Windows Foundation Presentation (WPF). Message boxes prompt users for a response, allowing the calling window to process that response. 03/15/2021
csharp
vb
message boxes [WPF]

How to open a message box (WPF .NET)

A message box is a dialog box that is used to quickly display information and optionally allow users to make decisions. Access to the message box is provided by the xref:System.Windows.MessageBox class. A message box is displayed modally. And the code that displays the message box is paused until the user closes the message box either with the close button or a response button.

The following illustration demonstrates the parts of a message box:

:::image type="content" source="media/how-to-open-message-box/diagram.png" alt-text="A figure that shows the parts of a message box for WPF.":::

  • A title bar with a caption (1).
  • A close button (2).
  • Icon (3).
  • Message displayed to the user (4).
  • Response buttons (5).

For presenting or gathering complex data, a dialog box might be more suitable than a message box. For more information, see Dialog boxes overview.

Display a message box

To create a message box, you use the xref:System.Windows.MessageBox class. The xref:System.Windows.MessageBox.Show%2A?displayProperty=nameWithType method lets you configure the message box text, title, icon, and buttons, shown in the following code:

:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="YesNoCancel"::: :::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="YesNoCancel":::

The xref:System.Windows.MessageBox.Show%2A?displayProperty=nameWithType method overloads provide ways to configure the message box. These options include:

  • Title bar caption
  • Message icon
  • Message text
  • Response buttons

Here are some more examples of using a message box.

  • Display an alert.

    :::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="AlertSimple"::: :::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="AlertSimple":::

    The previous code displays a message box like the following image:

    :::image type="content" source="media/how-to-open-message-box/alert-simple.png" alt-text="A simple message box for WPF that has no options configured.":::

    It's a good idea to use the options provided by the message box class. Using the same alert as before, set more options to make it more visually appealing:

    :::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="Alert"::: :::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="Alert":::

    The previous code displays a message box like the following image:

    :::image type="content" source="media/how-to-open-message-box/alert.png" alt-text="A warning message box for WPF that has an icon, caption, and text.":::

  • Display a warning.

    :::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="Warning"::: :::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="Warning":::

    The previous code displays a message box like the following image:

    :::image type="content" source="media/how-to-open-message-box/warning.png" alt-text="A simple message box for WPF that has displays a warning icon.":::

  • Ask the user a question.

    :::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="Prompt"::: :::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="Prompt":::

    The previous code displays a message box like the following image:

    :::image type="content" source="media/how-to-open-message-box/prompt.png" alt-text="A simple message box for WPF that prompts the user with a yes or no question.":::

Handle a message box response

The xref:System.Windows.MessageBox.Show%2A?displayProperty=nameWithType method displays the message box and returns a result. The result indicates how the user closed the message box:

:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="MessageBoxShow"::: :::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="MessageBoxShow":::

When a user presses the buttons at the bottom of the message box, the corresponding xref:System.Windows.MessageBoxResult is returned. However, if the user presses the ESC key or presses the Close button (#2 in the message box illustration), the result of the message box varies based on the button options:

Button options ESC or Close button result
Ok Ok
OkCancel Cancel
YesNo ESC keyboard shortcut and Close button disabled. User must press Yes or No.
YesNoCancel Cancel

For more information on using message boxes, see xref:System.Windows.MessageBox and the MessageBox sample.

See also