Click here to Skip to main content
15,881,204 members
Articles / Desktop Programming / WPF
Tip/Trick

All-Purpose Message Box in WPF

Rate me:
Please Sign up or sign in to vote.
4.53/5 (9 votes)
3 Mar 2014CPOL2 min read 48.3K   1.4K   25   6
Avoiding a cumbersome message box or input box that locks the application in WPF.

Introduction

Showing a Message Box, an Input Box or a Quick Popup Form may be achieved in great many different ways.
However, it is rather annoying to have to open or manage another window or special dialog just for that, after all, a message box functionality should really be quite simple, shouldn't it?

In addition, when you want further complexity in that message box or window and when you want it to communicate with the main window, it starts to get a little messy...

Background

So, what's new in this Tip?

As we stated above there are many ways to achieve the functionality of a Message Box,

Some examples would be:

  • Use a dialog, there's a great example of that in this CP article: WPF-Dialog-MessageBox-Manager
  • Use a simple MessageBox [System.Windows.MessageBox] as in the code sample below:
C#
MessageBoxResult result = 
  MessageBox.Show("Do you want to close this window?", 
  "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
    Application.Current.Shutdown();
}    

So, what's the problem?

Well, these approaches as well as several others will lock the application, and are quite cumbersome to style and manage. What we have in this Tip is a lightweight, flexible approach, that avoids the quite horrible locking behaviour of using external Win32-like approaches.

Using the code

So how is it accomplished?

Easy!

The basic idea is to add a Grid at the end of your XAML, initially with Visibility set to Collapsed. And then simply toggle the Visibility property to Visible when the MessageBox (or InputBox\Form etc) should appear. Upon confirmation (or cancellation), perform the needed tasks and reset the Visibility to Collapsed.

The Grid will have a semi transparent black background, and will contain the Form\MessageBox\InputBox etc in the middle, as any control.

The code and XAML are extremely simple, as you can see below.

Image 1

Image 2

XAML:

XML
<Window x:Class="DialogReplacement.dialogExample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="dialogExample" Height="500" Width="500">
    <Grid x:Name="gridMain">
        
        <StackPanel>
        <Button Margin="20" Width="200" Background="Black" 
            Foreground="White" Content="Click to toggle a Confirm Dialog" 
            Click="OpenMbox_Clicked" />
 
            <!-- 
            **********************
            Your Page content here
            **********************
            -->
            
        </StackPanel>
 
        <Grid x:Name="DialogReplacement" Visibility="Collapsed">
            <Grid Background="Black" Opacity="0.5"/>
 
            <Border
            MinWidth="250"
            Background="DarkGoldenrod" 
            BorderBrush="Black" 
            BorderThickness="1" 
            CornerRadius="0,65,0,65" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center">
 
                <StackPanel>
                    
                    <Button x:Name="btnClose" 
                            Margin="8" 
                            HorizontalAlignment="Left" 
                            Height="20" Width="20" 
                            Content="X" FontSize="12" 
                            FontFamily="Georgia" FontWeight="Bold" 
                            Foreground="WhiteSmoke" Background="Red" 
                            Click="mbox_cancel" />
                    
                    <StackPanel HorizontalAlignment="Center" Margin="0,-22,0,0">
                        
                        <Label FontFamily="Cambria" Content="Confirm Dialog" 
                               FontWeight="Bold" FontSize="20" />
                        <Label FontSize="14" FontWeight="Bold" Foreground="White" 
                               Content="Are you sure?"></Label>
                            
                    </StackPanel>
                    
                    <Button HorizontalAlignment="Right" x:Name="YesButton" 
                        Width="40" Margin="8" 
                        Padding="3,0,3,0" 
                        Content="Yes" Background="Olive" 
                        Foreground="White" 
                        Click="mbox_ok"/>
                       
                </StackPanel>
            </Border>
        </Grid>
    </Grid>
</Window>

Code Behind:

C#
namespace DialogReplacement
{    
    public partial class dialogExample : Window
    {
        public dialogExample()
        {
            InitializeComponent();
        }
 
        private void OpenMbox_Clicked(object sender, RoutedEventArgs e)
        {
            DialogReplacement.Visibility = System.Windows.Visibility.Visible;
        }
 
        private void mbox_ok(object sender, RoutedEventArgs e)
        {
            DialogReplacement.Visibility = System.Windows.Visibility.Collapsed;
        }
 
        private void mbox_cancel(object sender, RoutedEventArgs e)
        {
            DialogReplacement.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
} 

Example

Here's an example in which I almost deleted Chris' account...

Image 3

Image 4

Oh don't worry, I've clicked cancel ;)

History

  • Version 1.0.0.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
United States United States
Senior dish washing consultant for over 25 years!

Comments and Discussions

 
QuestionGood job Pin
1.21 Gigawatts4-Aug-14 2:39
1.21 Gigawatts4-Aug-14 2:39 
AnswerRe: Good job Pin
Joezer BH6-Aug-14 4:17
professionalJoezer BH6-Aug-14 4:17 
QuestionLooks nice Pin
johannesnestler3-Mar-14 1:00
johannesnestler3-Mar-14 1:00 
AnswerRe: Looks nice Pin
Joezer BH3-Mar-14 1:30
professionalJoezer BH3-Mar-14 1:30 
QuestionThe style is really nice Pin
Herbisaurus12-Aug-13 20:12
Herbisaurus12-Aug-13 20:12 
AnswerRe: The style is really nice Pin
Joezer BH12-Aug-13 20:14
professionalJoezer BH12-Aug-13 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.