articles

Home / DeveloperSection / Articles / Custom Message Box in C#.NET

Custom Message Box in C#.NET

Custom Message Box in C#.NET

Anonymous User 42468 08-Feb-2012

Hi, in this article I am going to explain how to create a custom message box in c#.net. As we know that by using MessageBox class to display message box but that is not looking beautiful. When we make project then sometimes we need to create a custom message box because the ordinary message box did not fill our requirements such as MessageBox which has checkbox control like Do Not Show Next time or have a gradient background color etc. In this demo, I will be told you, how to create a custom message box that has gradient background color. For completing this demo I used visual studio 2010 as IDE and c# as programing language.

In this demo, I had created two class frmShowMessage which display a message box and frmMain class which is used to test application. Then I had created two enumerations one is enumMessageIcon which contains Icon for Image and the other one is enumMessageButton which contains a type of buttons to be displayed.

frmShowMessage class have a static overloaded method named Show() which is used to display message. The first type accept one string parameter which is message needs to be displayed, the second type accept two string parameter, first is message to be displayed and the second one is message title and the last one is accepted four-parameter, first one is messageText, second one is message title, the third one is message icon and the last one is messageButton. You can create more overloaded methods according to your needs. Let’s start beginning this demo.

First, create a form named frmShowMessage and write down the following line of codes. I had used ImageList control which contains all images to be displayed on frmShowMessage and created two enumerations in the same namespace.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
 
namespace MessageBoxDemo
{
    public partial class frmShowMessage : Form
    {
        public frmShowMessage()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// Here I am overriding Paint method of form object
        /// and set it's background color as gradient. Here I am
        /// using LinearGradientBrush class object to make gradient
        /// color which comes in System.Drawing.Drawing2D namespace.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rect = this.ClientRectangle;
            LinearGradientBrush brush = new LinearGradientBrush(rect, Color.SkyBlue, Color.AliceBlue, 60);
            e.Graphics.FillRectangle(brush, rect);
            base.OnPaint(e);
        }
 
        /// <summary>
        /// setMessage method is used to display message
        /// on form and it's height adjust automatically.
        /// I am displaying message in a Label control.
        /// </summary>
        /// <param name="messageText">Message which needs to be displayed to user.</param>
        private void setMessage(string messageText)
        {
            int number = Math.Abs(messageText.Length / 30);
            if (number != 0)
                this.lblMessageText.Height = number * 25;
            this.lblMessageText.Text = messageText;
        }
 
        /// <summary>
        /// This method is used to add button on message box.
        /// </summary>
        /// <param name="MessageButton">MessageButton is type of enumMessageButton
        /// through which I get type of button which needs to be displayed.</param>
        private void addButton(enumMessageButton MessageButton)
        {
            switch (MessageButton)
            {
                case enumMessageButton.OK:
                    {
                        //If type of enumButton is OK then we add OK button only.
                        Button btnOk = new Button();  //Create object of Button.
                         btnOk.Text = "OK";  //Here we set text of Button.
                        btnOk.DialogResult = DialogResult.OK;  //Set DialogResult property of button.
                        btnOk.FlatStyle = FlatStyle.Popup;  //Set flat appearence of button.
                        btnOk.FlatAppearance.BorderSize = 0;
                        btnOk.SetBounds(pnlShowMessage.ClientSize.Width - 80, 5, 75, 25);  // Set bounds of button.
                        pnlShowMessage.Controls.Add(btnOk);  //Finally Add button control on panel.
                    }
                    break;
                case enumMessageButton.OKCancel:
                    {
                        Button btnOk = new Button();
                        btnOk.Text= "OK";                         btnOk.DialogResult = DialogResult.OK;
                        btnOk.FlatStyle= FlatStyle.Popup;
                        btnOk.FlatAppearance.BorderSize= 0;
                        btnOk.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25);
                        pnlShowMessage.Controls.Add(btnOk);
 
                        Button btnCancel = new Button();
                        btnCancel.Text = "Cancel";
                        btnCancel.DialogResult = DialogResult.Cancel;
                        btnCancel.FlatStyle = FlatStyle.Popup;
                        btnCancel.FlatAppearance.BorderSize = 0;
                        btnCancel.SetBounds((pnlShowMessage.ClientSize.Width - (btnOk.ClientSize.Width + 5 + 80)), 5, 75, 25);
                        pnlShowMessage.Controls.Add(btnCancel);
 
                    }
                    break;
                case enumMessageButton.YesNo:
                    {
                        Button btnNo = new Button();
                        btnNo.Text = "No";
                        btnNo.DialogResult = DialogResult.No;
                        btnNo.FlatStyle= FlatStyle.Popup;
                        btnNo.FlatAppearance.BorderSize= 0;
                        btnNo.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25);
                        pnlShowMessage.Controls.Add(btnNo);
 
                        Button btnYes = new Button();
                        btnYes.Text= "Yes";
                        btnYes.DialogResult= DialogResult.Yes;
                        btnYes.FlatStyle= FlatStyle.Popup;
                        btnYes.FlatAppearance.BorderSize= 0;
                        btnYes.SetBounds((pnlShowMessage.ClientSize.Width- (btnNo.ClientSize.Width + 5 + 80)), 5, 75, 25);
                        pnlShowMessage.Controls.Add(btnYes);
                    }
                    break;
                case enumMessageButton.YesNoCancel:
                    {
                        Button btnCancel = new Button();
                        btnCancel.Text= "Cancel";
                        btnCancel.DialogResult= DialogResult.Cancel;
                        btnCancel.FlatStyle= FlatStyle.Popup;
                        btnCancel.FlatAppearance.BorderSize= 0;
                        btnCancel.SetBounds((pnlShowMessage.ClientSize.Width- 70), 5, 65, 25);
                        pnlShowMessage.Controls.Add(btnCancel);
 
                        Button btnNo = new Button();
                        btnNo.Text= "No";
                        btnNo.DialogResult= DialogResult.No;
                        btnNo.FlatStyle= FlatStyle.Popup;
                        btnNo.FlatAppearance.BorderSize= 0;
                        btnNo.SetBounds((pnlShowMessage.ClientSize.Width- (btnCancel.ClientSize.Width + 5 + 80)), 5, 75, 25);
                        pnlShowMessage.Controls.Add(btnNo);
 
                        Button btnYes = new Button();
                        btnYes.Text= "Yes";
                        btnYes.DialogResult= DialogResult.No;
                        btnYes.FlatStyle= FlatStyle.Popup;
                        btnYes.FlatAppearance.BorderSize= 0;
                        btnYes.SetBounds((pnlShowMessage.ClientSize.Width- (btnCancel.ClientSize.Width + btnNo.ClientSize.Width + 10 + 80)), 5, 75, 25);
                        pnlShowMessage.Controls.Add(btnYes);
                    }
                    break;
            }
        }
 
        /// <summary>
        /// We can use this method to add image on message box.
        /// I had taken all images in ImageList control so that
        /// I can eaily add images. Image is displayed in
        /// PictureBox control.
        /// </summary>
        /// <param name="MessageIcon">Type of image to be displayed.</param>
        private void addIconImage(enumMessageIcon MessageIcon)
        {
            switch (MessageIcon)
            {
                case enumMessageIcon.Error:
                    pictureBox1.Image= imageList1.Images["Error"];  //Error is key name in imagelist control which uniqly identified images in ImageList control.
                    break;
                case enumMessageIcon.Information:
                    pictureBox1.Image= imageList1.Images["Information"];
                    break;
                case enumMessageIcon.Question:
                    pictureBox1.Image= imageList1.Images["Question"];
                    break;
                case enumMessageIcon.Warning:
                    pictureBox1.Image= imageList1.Images["Warning"];
                    break;
            }
        }
 
        #region Overloaded Show message to display message box.
 
        /// <summary>
        /// Show method is overloaded which is used to display message
        /// and this is static method so that we don't need to create
        /// object of this class to call this method.
        /// </summary>
        /// <param name="messageText"></param>
        internal static void Show(string messageText)
        {
            frmShowMessage frmMessage = new frmShowMessage();
            frmMessage.setMessage(messageText);
            frmMessage.addIconImage(enumMessageIcon.Information);
            frmMessage.addButton(enumMessageButton.OK);
            frmMessage.ShowDialog();
        }
 
        internal static void Show(string messageText, string messageTitle)
        {
            frmShowMessage frmMessage = new frmShowMessage();
            frmMessage.Text = messageTitle;
            frmMessage.setMessage(messageText);
            frmMessage.addIconImage(enumMessageIcon.Information);
            frmMessage.addButton(enumMessageButton.OK);
            frmMessage.ShowDialog();
        }
 
        internal static void Show(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
        {
            frmShowMessage frmMessage = new frmShowMessage();
            frmMessage.setMessage(messageText);
            frmMessage.Text= messageTitle;
            frmMessage.addIconImage(messageIcon);
            frmMessage.addButton(messageButton);
            frmMessage.ShowDialog();
        }
 
        #endregion
    }
 
    #region constant defiend in form of enumration which is used in showMessage class.
 
    internal enum enumMessageIcon
    {
        Error,
        Warning,
        Information,
        Question,
    }
 
    internal enum enumMessageButton
    {
        OK,
        YesNo,
        YesNoCancel,
        OKCancel
    }
 
    #endregion
}

  Now create frmMain form which is your main form or we can say startup form and write down the following code to test it.

using System;
using System.Windows.Forms;
 
namespace MessageBoxDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
 
        /// <summary>
        /// On Click event of button I am showing user defind message box.
        /// Here I am showing message box with three type of parameters.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            frmShowMessage.Show("This is message box which represent message with title, custome button and custom icon.""This is message title"enumMessageIcon.Question, enumMessageButton.OKCancel);
        }
    }
}

 After creating these two classes and writing code build your program and see output which is as follows.


Custom Message Box in C#.NET

Custom Message Box in C#.NET

Please write your valuable comments.

Thanks.


Updated 22-Feb-2020
I am a content writter !

Leave Comment

Comments

Liked By