Click here to Skip to main content
15,903,854 members
Articles / Programming Languages / C#

Custom Message Box

Rate me:
Please Sign up or sign in to vote.
1.02/5 (17 votes)
14 Nov 20063 min read 55.6K   1.5K   7   7
This article is written for the help of developing his own message box and can be used anywhere we want to use. This is written in C#. I think it will help to maximum developer.

Sample Image - CustomMessageBox.jpg


I am new for codeproject and this is my first article. I have written this article to help the developers who want to create own message box. So I think it would be most helpfull for the readers. I have written code in C#. And I will continue to inhancements in it if got any sugession from readers and I will most welcome to resolve query of readers.

How To Use

Basically I have written this article when I have required this in my project and try to find code. I have found a lot of code but I had not come way of coding. So finally decided to write my own code. And try to code for this article and got success in my way.

You can not directly use this article in your application but it will be helpful when
you want to generate your message box. Follow the following steps:

1. Add new form and named it.
2. Design GUI as per given image above.
3. Declare following in partial class
        private bool mouse_is_down = false;
        private Point mouse_pos;
4. On Load event paste this code:
        private void MsgBox_load(object sender, EventArgs e)
            if (!btnCancel.Visible)
                btnOK.Left = (this.Width - btnOK.Width) / 2;
                btnOK.Left = this.Width / 2 - btnOK.Width - 5;
            btnCancel.Left = btnOK.Right + 5;


  # region "Apperiance"

        public void ResizeForm()

            if (lblMessage.Width > 168)
                this.Width = lblMessage.Width + 88;

            int lblWidth = this.lblMessage.Width;
            this.lblMessage.AutoSize = false;
            this.lblMessage.Width = lblWidth;


      public void MyMessageBox(string message)
            MsgBox msgbox = new MsgBox();
            lblMessage.Text = message;


5.  Code it for set button's visibility and Setting  message title

     public void buttonVisibility(bool OK, bool cancelstatus, bool checkbox,string msgTitle)
            btnOK.Visible = OK;
            btnCancel.Visible = cancelstatus;
            chkAskagain.Visible = checkbox;
            lblTitle.Text = msgTitle;

6.  write this code for OK and Cancel action

    private void btnCancel_Click(object sender, EventArgs e)

        private void btnOK_Click(object sender, System.EventArgs e)
            this.DialogResult = DialogResult.OK;


7. This code is for moving the form

    private void pbMessage_MouseDown(object sender, MouseEventArgs e)
            mouse_pos.X = e.X;
            mouse_pos.Y = e.Y;
            mouse_is_down = true;

        private void pbMessage_MouseUp(object sender, MouseEventArgs e)
            mouse_is_down = false;

        private void pbMessage_MouseMove(object sender, MouseEventArgs e)
            if (mouse_is_down)
                Point current_pos = Control.MousePosition;
                current_pos.X = current_pos.X - mouse_pos.X;
                current_pos.Y = current_pos.Y - mouse_pos.Y;
                this.Location = current_pos;

8.  And last is for closing the msgbox.

     private void pictureBox2_Click(object sender, EventArgs e)

How to call this message box in your application:

Write this code in that partial class where you have to call message box

private MsgBox msgbox = new MsgBox();

Finally write for calling of messagebox

            msgbox.MyMessageBox("Hi!This is Demo Message Box");
            msgbox.buttonVisibility(true, true, true,"Demo Message Box");
            DialogResult msgdresult = msgbox.ShowDialog();





This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer (Senior) TMG India (P) Ltd.
India India
I am Avadhesh Kumar Maurya living in Gurgaon and working in Microsoft Technologies (,, Javascript, MVC. Sql Server, VB6.0, WCF, Ajax) from last 8 years. I loves reading Science Fiction,and listening to soft music.

Have a nice day!

Avadhesh Kumar Maurya

Comments and Discussions

GeneralMy vote of 1 Pin
ibuza18-Feb-10 21:57
ibuza18-Feb-10 21:57 
GeneralPoor Job Pin
Nate Shoffner3-Jul-09 8:03
Nate Shoffner3-Jul-09 8:03 
GeneralVery POOR Pin
crazy_mads14-Nov-06 23:42
crazy_mads14-Nov-06 23:42 
GeneralRe: Very POOR Pin
Avadhesh Kumar Maurya18-May-07 2:43
Avadhesh Kumar Maurya18-May-07 2:43 
Hello dear,

Hint is sufficient. I do'nt want to made spoon feeding.
GeneralRe: Very POOR Pin
Dave Kreskowiak21-Jul-07 15:11
mveDave Kreskowiak21-Jul-07 15:11 
GeneralPOOR Pin
Ivan Mladenović14-Nov-06 23:27
Ivan Mladenović14-Nov-06 23:27 
QuestionVoted yourself a 5 ? Pin
Jonathan [Darka]14-Nov-06 23:21
professionalJonathan [Darka]14-Nov-06 23:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.