Display a message box and get the message box return value. : MessageBox « Windows Presentation Foundation « VB.Net 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.vb
Imports System
Imports System.Windows

Namespace MessageBoxSample
  Public Partial Class MainWindow
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub showMessageBoxButton_Click(sender As Object, e As RoutedEventArgs)
      Dim owner As Window = (If(CBool(ownerCheckBox.IsChecked), Me, Nothing))
      Dim messageBoxText As String = Me.messageBoxText.Text
      Dim caption As String = Me.caption.Text
      Dim button As MessageBoxButton = CType([Enum].Parse(GetType(MessageBoxButton), Me.buttonComboBox.Text), MessageBoxButton)
      Dim icon As MessageBoxImage = CType([Enum].Parse(GetType(MessageBoxImage), Me.imageComboBox.Text), MessageBoxImage)
      Dim defaultResult As MessageBoxResult = CType([Enum].Parse(GetType(MessageBoxResult), Me.defaultResultComboBox.Text), MessageBoxResult)
      Dim options As MessageBoxOptions = CType([Enum].Parse(GetType(MessageBoxOptions), Me.optionsComboBox.Text), MessageBoxOptions)

      Dim result As MessageBoxResult
      If owner Is Nothing Then
        result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options)
      Else
        result = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, _
          options)
      End If

      resultTextBlock.Text = "Result = " & result.ToString()
    End Sub
  End Class
End Namespace
WPF Display A Message Box And Get The Message Box Return Value








16.55.MessageBox
16.55.1.Message Only MessageBoxMessage Only MessageBox
16.55.2.MessageBox with Message and HeaderMessageBox with Message and Header
16.55.3.Set Message, Header, and Button for MessageBoxSet Message, Header, and Button for MessageBox
16.55.4.Customize Message, Header, Button, and Image for MessageBoxCustomize Message, Header, Button, and Image for MessageBox
16.55.5.Display a message box and get the message box return value.Display a message box and get the message box return value.