Click here to Skip to main content
11,705,098 members (49,097 online)
Click here to Skip to main content

Custom Message Box

, 14 Nov 2006 34.4K 1.1K 6
Rate this:
Please Sign up or sign in to vote.
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

Introduction

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)
        {
            ResizeForm();
            if (!btnCancel.Visible)
                btnOK.Left = (this.Width - btnOK.Width) / 2;
            else
                btnOK.Left = this.Width / 2 - btnOK.Width - 5;
            btnCancel.Left = btnOK.Right + 5;
         }

 //ResizeForm()

  # 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;

        }
        #endregion

      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)
        {
            this.Hide();
        }

        private void btnOK_Click(object sender, System.EventArgs e)
        {
            this.Hide();
            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)
        {
            this.Close();
        }

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();

 


 


 

License

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

Share

About the Author

Avadhesh Kumar Maurya
Software Developer (Senior) TMG India (P) Ltd.
India India
I am Avadhesh Kumar Maurya living in Gurgaon and working in Microsoft Technologies (Asp.net, C#.net, 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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
ibuza18-Feb-10 21:57
memberibuza18-Feb-10 21:57 
GeneralPoor Job Pin
Nate Shoffner3-Jul-09 8:03
memberNate Shoffner3-Jul-09 8:03 
GeneralVery POOR Pin
crazy_mads14-Nov-06 23:42
membercrazy_mads14-Nov-06 23:42 
GeneralRe: Very POOR Pin
Avadhesh Kumar Maurya18-May-07 2:43
memberAvadhesh Kumar Maurya18-May-07 2:43 
GeneralRe: Very POOR Pin
Dave Kreskowiak21-Jul-07 15:11
mvpDave Kreskowiak21-Jul-07 15:11 
GeneralPOOR Pin
Ivan Mladenovic14-Nov-06 23:27
memberIvan Mladenovic14-Nov-06 23:27 
QuestionVoted yourself a 5 ? Pin
Jonathan [Darka]14-Nov-06 23:21
memberJonathan [Darka]14-Nov-06 23:21 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 15 Nov 2006
Article Copyright 2006 by Avadhesh Kumar Maurya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid