Thursday, January 29, 2009

WPF MessageBox

Introduction

When we open MessageBox in a WPF application, it looks are very similar to that of a Window Form. We cannot inherits or apply style to MessageBox. Using MessageBox in a WPF application with a rich user interface can look bad. So in this article we will create our own WPFMessageBox with rich user interface.

Using the code

WPFMessageBox is very similar to MessageBox in WPF with added functionality and rich user interface. We can apply two different skin to WPFMessageBox just by setting Skin property.

WPFMessageBox with Black Skin

WPFMessageBox2

WPFMessageBox with Blue Skin

WPFMessageBox1

Code to apply Black Skin and Blue Skin to WPFMessageBox is as below

'Code to apply Black Skin
WpfMessageBox.Skin = DisplaySkin.Black
'Code to apply Blue Skin
WpfMessageBox.Skin = DisplaySkin.Blue

Just like MessageBox, WPFMessageBox also has Show() method with various overloads to set caption, text, icon, buttons and default result.

Various overloaded functions for WPFMessageBox are shown in following code.


Function Show(ByVal text As String) As WpfMessageBoxResult
Function Show(ByVal text As String, ByVal caption As String) As WpfMessageBoxResult 
Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton) As WpfMessageBoxResult
Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton, ByVal icon As WpfMessageBoxImage) As WpfMessageBoxResult
Function Show(ByVal text As String, ByVal caption As String, ByVal button As WpfMessageBoxButton, ByVal icon As WpfMessageBoxImage, ByVal defaultResult As WpfMessageBoxResult) As WpfMessageBoxResult

All these overload functions are shared. So we can use them without creating their object.

WpfMessageBox.Show("Message box with a text and Caption", "WPF Message Box")

Above example will show WPFMessageBox with “Message box with a text and Caption” as text and “WPF Message Box” as caption.

WpfMessageBox.Show("Message with Yes and No", "WPF Message Box", WpfMessageBoxButton.YesNo)

Above example will show “Message with Yes and No” as text, “WPF Message Box” as caption and will contain Yes and No button.

Code for WPFMessageBox.Show have similar overload parameters to MessageBox.Show with great user interface. So one familiar to MessageBox can easily use WPFMessageBox

You can download the code from below link


Download Source Code