Click here to Skip to main content
Click here to Skip to main content

All-Purpose Message Box in WPF

, 3 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
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?

That's 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.

XAML:

<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:

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...

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

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)

Share

About the Author

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

Comments and Discussions

 
QuestionGood job Pinmember1.21 Gigawatts4-Aug-14 3:39 
AnswerRe: Good job PinprofessionalCanny Brisk6-Aug-14 5:17 
QuestionLooks nice Pinmemberjohannesnestler3-Mar-14 2:00 
AnswerRe: Looks nice PinprofessionalCanny Brisk3-Mar-14 2:30 
QuestionThe style is really nice PinmemberHerbisaurus12-Aug-13 21:12 
AnswerRe: The style is really nice PinprofessionalMaimonides12-Aug-13 21:14 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 3 Mar 2014
Article Copyright 2013 by _Canny_Brisk_
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid