Search content within the blog

Tuesday, July 26, 2011

WPF Message box

The sample demonstrates the use of message box in WPF. it also shows the various options available for the WPF message box parameters like "caption", "image to be shown", "MessageBoxText".


The code/example is self explanatory .Just copy paste the code in the XAML and it's code behind file.


XAML Code
<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>






XAML.CS code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MessageBoxSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

void showMessageBoxButton_Click(object sender, RoutedEventArgs e)
{
// Configure the message box
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);

// Show message box, passing the window owner if specified
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);
}

// Show the result
resultTextBlock.Text = "Result = " + result.ToString();
}
}
}

No comments:

Post a Comment