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

Custom Message Box in C#.NET


C# C# 
Ratings:
2 Comment(s)
 23589  View(s)
Rate this:

Custom Message Box in C#.NET

Hi, in this article I am going to explain that how to create 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 custom message box because ordinary message box did not fill our requitements such as MessageBox which have check box control like Do Not Show Next time or have a gradient back ground color etc. In this demo I will told you, how to create custom message box which have gradient back ground color. For completing this demo I used visual studio 2010 as IDE and c# as programing language.

In this demo I had create two class frmShowMessage which display 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 other one is enumMessageButton which contains type of buttons to be displayed.

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

First create a form named frmShowMessage and write down following line of codes. I had used ImageList control which contains all images to be displayed on frmShowMessage and created two enumerations in 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 start up form and write down 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.


DLL path hide

By parag patel on   4 years ago
  try
            {

   
            string lgin = "";
            float f = AFFile.FOpen(@"d:\\localhost.dll", "r");
            if (ATFloat.IsTrue(f))
            {
                while (ATFloat.IsFalse(AFFile.FEof(f)))
                {
                    lgin = lgin + AFFile.FGets(f);
          
                }
            }
            else
            {
                AFStr.printf("ERROR: file can not be found (does not exist)");
            }
            AFFile.FClose(f);

            if (lgin.Trim() == ":)")
            {

how can hide
i creat dll then open in notpad [  YException Exception Show .cctor    #d : \ \ l o c a l h o s t . d l l  r  ]
how can hide this pls help   k_info108@yahoo.com

MessageBox C#

By Junior Lopes on   3 years ago
Friend, Good Afternoon

Excuse the ignorance, but how do I use the buttons on this message box.
For example, I ask you to delete and as the answer or not to proceed with the action.

thank you

Junior Lopes / Brazil

Don't want to miss updates? Please click the below button!

Follow MindStick