Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#

Custom MessageBox Control for Silverlight 3

Rate me:
Please Sign up or sign in to vote.
4.20/5 (16 votes)
19 Sep 2009CPOL4 min read 92.6K   3.5K   21  
Creating a user control for displaying a custom messagebox in Silverlight 3.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace MessageBoxControl
{
    public partial class MessageBoxChildWindow : ChildWindow
    {
        public delegate void MessageBoxClosedDelegate(MessageBoxResult result);
        public event MessageBoxClosedDelegate OnMessageBoxClosed;
        public MessageBoxResult Result { get; set; }

        public MessageBoxChildWindow()
        {
            InitializeComponent();
            this.HasCloseButton = false;
            this.Closed += new EventHandler(MessageBoxChildWindow_Closed);
        }

        public MessageBoxChildWindow(string title, string message, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            InitializeComponent();
            this.HasCloseButton = false;
            this.Closed += new EventHandler(MessageBoxChildWindow_Closed);

            this.Title = title;
            this.txtMsg.Text = message;
            DisplayButtons(buttons);
            DisplayIcon(icon);
        }

        private void DisplayButtons(MessageBoxButtons buttons)
        {
            switch (buttons)
            {
                case MessageBoxButtons.Ok:
                    btnCancel.Visibility = Visibility.Collapsed;
                    btnNo.Visibility = Visibility.Collapsed;
                    btnYes.Visibility = Visibility.Visible;
                    btnYes.Content = "Ok";
                    break;

                case MessageBoxButtons.OkCancel:
                    btnCancel.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Collapsed;
                    btnYes.Visibility = Visibility.Visible;
                    btnYes.Content = "Ok";
                    break;

                case MessageBoxButtons.YesNo:
                    btnCancel.Visibility = Visibility.Collapsed;
                    btnNo.Visibility = Visibility.Visible;
                    btnYes.Visibility = Visibility.Visible;
                    break;

                case MessageBoxButtons.YesNoCancel:
                    btnCancel.Visibility = Visibility.Visible;
                    btnNo.Visibility = Visibility.Visible;
                    btnYes.Visibility = Visibility.Visible;
                    break;
            }
        }

        private void DisplayIcon(MessageBoxIcon icon)
        {
            switch (icon)
            {
                case MessageBoxIcon.Error:
                    imgIcon.Source = new BitmapImage(new Uri("error.png", UriKind.Relative));
                    break;

                case MessageBoxIcon.Information:
                    imgIcon.Source = new BitmapImage(new Uri("info.png", UriKind.Relative));
                    break;

                case MessageBoxIcon.Question:
                    imgIcon.Source = new BitmapImage(new Uri("question.png", UriKind.Relative));
                    break;

                case MessageBoxIcon.Warning:
                    imgIcon.Source = new BitmapImage(new Uri("warning.png", UriKind.Relative));
                    break;

                case MessageBoxIcon.None:
                    imgIcon.Source = new BitmapImage(new Uri("info.png", UriKind.Relative));
                    break;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (btnYes.Content.ToString().ToLower().Equals("yes") == true)
            {
                //yes button
                this.Result = MessageBoxResult.Yes;
            }
            else
            {
                //ok button
                this.Result = MessageBoxResult.OK;
            }

            this.Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Result = MessageBoxResult.Cancel;
            this.Close();
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            this.Result = MessageBoxResult.No;
            this.Close();
        }

        private void MessageBoxChildWindow_Closed(object sender, EventArgs e)
        {
            if (OnMessageBoxClosed != null)
                OnMessageBoxClosed(this.Result);
        }
    }

    public enum MessageBoxButtons
    {
        Ok, YesNo, YesNoCancel, OkCancel
    }

    public enum MessageBoxIcon
    {
        Question, Information, Error, None, Warning
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Asset Technology Group
Egypt Egypt
I'm a professional components designer, web developer, UX engineer and 3d designer as well, I'm 4 years experienced .net software engineer and 7 years experienced 3d designer using 3D Max. I'm very interested in RIA technologies, prototyping and UX engineering.

Ahmed Said
Senior .Net Software Engineer

Comments and Discussions