RadMessageBox

2 posts, 0 answers
  1. Christian
    Christian avatar
    12 posts
    Member since:
    Aug 2010

    Posted 28 Mar 2011 Link to this post

    Requirements

    RadControls version

     Q2 / Q3 2010 & Q1 2011

    .NET version

    .NET 4 

    Visual Studio version

    VS 2010 

    programming language

     C#

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION

    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. This article is also available on The Code Project


    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 oxes 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 forth 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 standard .Net MessageBoxImage enumeration is used to implement a method signature comparable to the standard implementation.

    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)

  2. George
    Admin
    George avatar
    1332 posts

    Posted 29 Mar 2011 Link to this post

    Hi Christian,

     
    Thank you for this project! I am glad to update your telerik points.

    Regards,
    George
    the Telerik team
Back to Top