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

Tagged as

Go to top

RadMessageBox for the Telerik WPF Controls

, 29 Mar 2011
Rate this:
Please Sign up or sign in to vote.
A MessageBox for the Telerik WPF Controls library following the design pattern of the WPF standard MessageBox implementation

Editorial Note

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

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:

/// <summary>
/// Show a modal rad alert box
/// This static method was implemented following the Microsoft pattern 
/// for the standard WPF MessageBox
/// but is using the telerik RadWindow internally
/// </summary>
/// <param name="ctrlOwner">The owner window if needed otherwise null</param>
/// <param name="strMessage">The message to display</param>
/// <param name="strCaption">The title of the message box window. 
/// (Parameter is optional)</param>
/// <param name="button">The buttons the dialog should have - Default is ok</param>
/// <param name="image">The image the dialog should hold - Default is Warning</param>
/// <returns>A message box result enum with the result of the dialog</returns>
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:

Member name Icon
None none.png
Hand stop.png
Question question.png
Exclamation Warning.png
Asterisk information.png
Stop stop.png
Error stop.png

Warning

This is the default parameter
Warning.png
Information information.png

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.

License

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

Share

About the Author

Christian Kleinheinz
CEO MasterSoft Software Solutions Ltd.
Austria Austria

Chris is one of the founders of MasterSoft Software Solutions Ltd., a software development company located in Austria.

He has worked as a C++ developer in the fields of 3D computer simulations and studied Business Informatics before the company opening.

Chris is using C++/C#/ASP with .Net 4 intensely but also has experience in Java or PHP.

To get additional information about him or his company feel free to visit the webpage of his company or visit his profiles in the social networks like G+, Twitter or Xing for all german networkers.

You are also welcome to visit the webpage for all hosting services his company offers at MasterServer

Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinmemberakramKamal12-Dec-12 0:21 

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 | Mobile
Web02 | 2.8.140922.1 | Last Updated 29 Mar 2011
Article Copyright 2011 by Christian Kleinheinz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid