Display a message box and get the message box return value. : MessageBox « Windows Presentation Foundation « C# / CSharp Tutorial






<Window x:Class="MessageBoxSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MessageBoxSample" Height="300" Width="500">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"></ColumnDefinition>
      <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <Label Grid.Column="0" Grid.Row="0">Associate with Owner Window?</Label>
    <CheckBox Grid.Column="1" Grid.Row="0" Name="ownerCheckBox"></CheckBox>

    <Label Grid.Column="0" Grid.Row="1">messageBoxText:</Label>
    <TextBox Grid.Column="1" Grid.Row="1" Name ="messageBoxText">MessageBoxText</TextBox>

    <Label Grid.Column="0" Grid.Row="2">caption:</Label>
    <TextBox Grid.Column="1" Grid.Row="2" Name="caption">Caption</TextBox>

    <Label Grid.Column="0" Grid.Row="3">button:</Label>
    <ComboBox Grid.Column="1" Grid.Row="3" Name="buttonComboBox">
      <ComboBoxItem IsSelected="True">OK</ComboBoxItem>
      <ComboBoxItem>OKCancel</ComboBoxItem>
      <ComboBoxItem>YesNo</ComboBoxItem>
      <ComboBoxItem>YesNoCancel</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="4">icon:</Label>
    <ComboBox Grid.Column="1" Grid.Row="4" Name="imageComboBox">
      <ComboBoxItem>Asterisk</ComboBoxItem>
      <ComboBoxItem>Error</ComboBoxItem>
      <ComboBoxItem>Exclamation</ComboBoxItem>
      <ComboBoxItem>Hand</ComboBoxItem>
      <ComboBoxItem>Information</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>Question</ComboBoxItem>
      <ComboBoxItem>Stop</ComboBoxItem>
      <ComboBoxItem>Warning</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="5">defaultResult:</Label>
    <ComboBox Grid.Column="1" Grid.Row="5" Name="defaultResultComboBox">
      <ComboBoxItem>Cancel</ComboBoxItem>
      <ComboBoxItem>No</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>OK</ComboBoxItem>
      <ComboBoxItem>Yes</ComboBoxItem>
    </ComboBox>

    <Label Grid.Column="0" Grid.Row="6">options</Label>
    <ComboBox Grid.Column="1" Grid.Row="6" Name="optionsComboBox">
      <ComboBoxItem>DefaultDesktopOnly</ComboBoxItem>
      <ComboBoxItem IsSelected="True">None</ComboBoxItem>
      <ComboBoxItem>RightAlign</ComboBoxItem>
      <ComboBoxItem>RtlReading</ComboBoxItem>
      <ComboBoxItem>ServiceNotification</ComboBoxItem>
    </ComboBox>

    <Button Grid.Column="1" Grid.Row="7" Name="showMessageBoxButton" Click="showMessageBoxButton_Click">Show MessageBox</Button>

    <StatusBar Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="8" >
      <StatusBarItem>
        <TextBlock Name="resultTextBlock">Ready</TextBlock>
      </StatusBarItem>
    </StatusBar>

  </Grid>

</Window>


//File:Window.xaml.cs
using System;
using System.Windows;

namespace MessageBoxSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void showMessageBoxButton_Click(object sender, RoutedEventArgs e)
        {
            Window owner = ((bool)ownerCheckBox.IsChecked ? this : null);
            string messageBoxText = this.messageBoxText.Text;
            string caption = this.caption.Text;
            MessageBoxButton button = (MessageBoxButton)Enum.Parse(typeof(MessageBoxButton), this.buttonComboBox.Text);
            MessageBoxImage icon = (MessageBoxImage)Enum.Parse(typeof(MessageBoxImage), this.imageComboBox.Text);
            MessageBoxResult defaultResult = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), this.defaultResultComboBox.Text);
            MessageBoxOptions options = (MessageBoxOptions)Enum.Parse(typeof(MessageBoxOptions), this.optionsComboBox.Text);

            MessageBoxResult result;
            if (owner == null)
            {
                result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
            }
            else
            {
                result = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
            }
            
            resultTextBlock.Text = "Result = " + result.ToString();
        }
    }
}
WPF Display A Message Box And Get The Message Box Return Value








24.67.MessageBox
24.67.1.Message Only MessageBoxMessage Only MessageBox
24.67.2.MessageBox with Message and HeaderMessageBox with Message and Header
24.67.3.Set Message, Header, and Button for MessageBoxSet Message, Header, and Button for MessageBox
24.67.4.Customize Message, Header, Button, and Image for MessageBoxCustomize Message, Header, Button, and Image for MessageBox
24.67.5.Display a message box and get the message box return value.Display a message box and get the message box return value.
24.67.6.Display information with WPF MessageBox
24.67.7.WPF message box
24.67.8.Question MessageBox