Introduction
Telerik released a set of professional .NET GUI components with the Telerik Ultimate Collection for .NET.
This article and the source code attached are implementing a RadMessageBox
class which uses the same development style as the original WPF MessageBox
implemented by the Microsoft development team.
Background
Our company purchased the Telerik Ultimate Collection for .NET some time ago. When I was refactoring some of our WPF applications using the Telerik WPF controls, I had to exchange some standard .NET message boxes to get rid of the standard WPF GUI style.
The Telerik Library provides an Alert message box, a confirm message box and a prompt message box. When integrating them into the application, I came across two things that I wanted to be implemented in a slightly different way.
The first thing was that the Rad message box, shown by the RadWindow.Alert()
method, is always centered on the primary screen. This is fine if your application just uses one screen, but modern and professional software products should utilize more than one.
The problem is discussed in this telerik forums post.
I wanted the message box to be opened centered to its owner window instead of showing the message box on the screen where the main GUI window is located.
When looking at the standard WPF MessageBox
, the owner window can be set by passing the reference as first parameter of the overloaded static Show
method of the MessageBox
.
You can find the MSDN documentation of the MessageBox
here.
The RadWindow.Alert(...)
method does not provide a way to set an owner window nor a solution to get and configure the underlying RadWindow
instance.
The second thing was the fact that the telerik Alert and Notify dialogs are implemented in a different way than the original WPF message box.
The telerik library introduces the DialogParameters
class to further configure the message boxes, but currently there is no way to set the owner window.
This means that you will need to change your code for every occurrence of the MessageBox
when refactoring the application.
That’s why I decided to implement a new RadMessageBox
class to extend the telerik library. I wanted the message box to be opened centered to its owner window with a method signature similar to the standard WPF message boxes.
Class Description
The class is a small and simple RadWindow
which implements two properties defining the image and the buttons to be shown on the window. Since this is a normal RadWindow
, it may be either opened like any other window, or by using the static Show
methods where the parameter list is equal to the parameter list of the standard WPF message box implementation.
The implementation of this method looks like this:
public static MessageBoxResult Show(ContentControl ctrlOwner, string strMessage,
string strCaption = null, MessageBoxButton button = MessageBoxButton.OK,
MessageBoxImage image = MessageBoxImage.Warning)
{
try
{
RadMessageBox dlg = new RadMessageBox();
dlg.DialogImage = image;
dlg.Buttons = button;
if(strCaption != null)
dlg.Header = strCaption;
dlg.txtMessage.Text = strMessage;
if (ctrlOwner != null)
{
dlg.Owner = ctrlOwner;
}
dlg.ShowDialog();
MessageBoxResult res = dlg.Result;
return (res);
}
catch (Exception err)
{
System.Diagnostics.Trace.TraceError(err.ToString());
return (MessageBoxResult.None);
}
}
As you can see, the static
method simply uses the dialog instance to display the message.
The first parameter of this method is a ContentControl
reference which is the owner window of the message box.
Since this is a ContentControl
reference and not a simple Window
or RadWindow
reference, you can use both types of windows as owners.
The second parameter is the message text which should be displayed in the message box.
The third parameter is the window title the message box should have. This parameter is null
for default.
The fourth parameter is the type of buttons the message box should display. I have used the MessageBoxButton
enumeration in order to keep the signature of the function equivalent to the standard WPF MessageBox
.
The enumeration can be one of the values listed in the table below which can be found at this MSDN website.
Member name | Description |
OK | The message box contains an OK button.
This is the default parameter.
|
OKCancel | The message box contains OK and Cancel buttons. |
AbortRetryIgnore | The message box contains Abort, Retry, and Ignore buttons. |
YesNoCancel | The message box contains Yes, No, and Cancel buttons. |
YesNo | The message box contains Yes and No buttons. |
RetryCancel | The message box contains Retry and Cancel buttons. |
The firth parameter is the message box image which should be displayed. Again the The standard .NET MessageBoxImage
enumeration is used to implement a method signature comparable to the standard implementation.
The enumeration name and the icon which will be shown on the dialog can be seen in the table below:
As you can see in the table above, the message box icons are sometimes equal for different enumeration names. This is not a bug or a mistake. If we do look at the definition of this Microsoft enumeration, we can see that some of the enumeration names do have an equal value.
namespace System.Windows
{
public enum MessageBoxImage
{
None = 0,
Error = 16,
Hand = 16,
Stop = 16,
Question = 32,
Exclamation = 48,
Warning = 48,
Asterisk = 64,
Information = 64,
}
}
Using the Code
To use the class, simply add the RadWrapper.dll assembly located in the /bin/Release directory from the code attached to your project. Please be aware that the class uses the RadWindow
base class, which is part of the Telerik WPF controls library. This means that you will need the Telerik WPF controls library installed on your system.
Once added to your project, you can use the RadMesssageBox
just the way you are familiar with from using the standard version of the WPF library.
To show a message box with an information icon and only an ok button, your call of the Show
method will look like shown below, provided that the current object (this
) is a standard Window
or a RadWindow
.
RadMessageBox.Show(this, "My Message", "Window title",
MessageBoxButton.OK, MessageBoxImage.Information);
To use the message box for a confirmation question, you can use the return value of the static Show
method which is a standard MessageBoxResult
enumeration.
This enumeration can be one of the values shown in the table below, which can be found at the MSDN library pages.
Member name | Description |
None | The message box returns no result. |
OK | The result value of the message box is OK. |
Cancel | The result value of the message box is Cancel. |
Yes | The result value of the message box is Yes. |
No | The result value of the message box is No. |
Conclusion
The team of Telerik has done a great job when implementing their library. The class in this project is intended to speed up the refactoring work when integrating the Telerik library into the project.
The class was implemented and tested with the Q2 and the Q3 Release 2010 of the Telerik WPF controls library.