Windows Develop Bookmark and Share   
 index > Windows Forms General > MessageBox in c#
 

MessageBox in c#

I want to show messagebox to the user, such that the user cannot refuse to confirm the message box. User should not be allowed to do anything else in the screen until he confirms the messagebox.

This is a windows based c# application.

The main thing is, even if i use windows message box. Some times it is hiding behind some screen. But for my case, i want message box to be on top most whenever it appears.

I am using some other third party applications, which over rides my message box. I want to overcome this.

How to do this...

karthik.sr  Wednesday, September 09, 2009 8:05 AM
Create custom message box, i think it may resolve your problem,the message box that i am using in my application is given below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace BDSUtilityService
{
    public partial class BDSMessageBox : Form
    {
        static BDSMessageBox newMessageBox;

        string YesStr = "&Yes       ";
        string NoStr =  "&No       ";
        #region public enumeration
        //Enum MessageBox
        public enum MessageBox_Type
        {
            INFORMATIONBOX,
            WARRNINGBOX,
            ERRORBOX,
            QUESTIONBOX
        }
        private MessageBox_Type m_Type = MessageBox_Type.INFORMATIONBOX;

        //Enum Buttons
        public enum MessageBox_ButtonType
        {
            OK,
            CANCEL,
            BOTH
        }
        private MessageBox_ButtonType m_Button = MessageBox_ButtonType.BOTH;

        //Box Button Alignment
        public enum MessageBox_ButtonAlign
        {
            RIGHTLEFT,
            LEFT,
            RIGHT,
            CENTER
        }
        private MessageBox_ButtonAlign m_Align = MessageBox_ButtonAlign.CENTER;
        #endregion

        #region constructors
        public BDSMessageBox()
        {
            InitializeComponent();
            btnOK.Focus();
        }

        public BDSMessageBox(string strMsg,string strCaption)
        {
            InitializeComponent();
            this.lblMessage.Text = strMsg;
            this.lblTitle.Text  = strCaption;
            this.lblMessage.SelectionStart = strMsg.Length;
        }

        public BDSMessageBox(string strMsg, string strCaption, string strYes , string strNo )
        {
            InitializeComponent();
            this.lblMessage.Text = strMsg;
            this.lblTitle.Text = strCaption;
            this.lblMessage.SelectionStart = strMsg.Length;
            YesStr = strYes;
            NoStr = strNo;
        }
        #endregion

        #region MessageBox Properties

        //Box Type
        [Description("Sets the Type of Message Box"),
         Category("Appearance"),
         DefaultValue(0)]
        public MessageBox_Type MessageBoxType
        {
            get { return m_Type; }
            set
            {

                m_Type = value;
                switch (m_Type)
                {
                    case MessageBox_Type.INFORMATIONBOX:
                        pbImage.Image = UtilityResources.INFORMATION;
                        break;
                    case MessageBox_Type.WARRNINGBOX:
                        pbImage.Image = UtilityResources.WARRNING;
                        break;
                    case MessageBox_Type.ERRORBOX:
                        pbImage.Image = UtilityResources.ERROR;
                        break;                   
                    default:
                        pbImage.Image = UtilityResources.QUESTION;
                        btnOK.Text = YesStr; // "&Yes      ";
                        btnCancel.Text = NoStr;//"&No       ";
                        break;
                }

            }
        }

        //Button Alignment
        [Description("Sets the Alignment of Buttons"),
         Category("Appearance"),
         DefaultValue(0)]
        public MessageBox_ButtonAlign MessageBoxButtonAlign
        {
            get { return m_Align; }
            set
            {

                m_Align = value;
                switch (m_Align)
                {
                    case MessageBox_ButtonAlign.RIGHTLEFT:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(12, 140);
                                btnCancel.Location = new Point(463, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(239, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(463, 140);
                                break;
                        }
                        break;
                    case MessageBox_ButtonAlign.RIGHT:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(375, 140);
                                btnCancel.Location = new Point(463, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(463, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(463, 140);
                                break;
                        }
                        break;
                    case MessageBox_ButtonAlign.LEFT:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(12, 140);
                                btnCancel.Location = new Point(101, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(12, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(12, 140);
                                break;
                        }
                        break;

                    default:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(204, 140);
                                btnCancel.Location = new Point(295, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(239, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(239, 140);
                                break;
                        }
                        break;
                }

            }
        }

        //MessageBox Buttons Type
        [Description("Sets the type of Buttons"),
         Category("Appearance"),
         DefaultValue(0)]
        public MessageBox_ButtonType MessageBoxButtonType
        {
            get { return m_Button; }
            set
            {

                m_Button = value;
                switch (m_Button)
                {
                    case MessageBox_ButtonType.BOTH:
                        btnCancel.Visible = true;
                        btnOK.Visible = true;
                        break;
                    case MessageBox_ButtonType.CANCEL:
                        btnOK.Visible = false;
                        break;
                    default:
                        btnCancel.Visible = false;
                        break;
                }
            }
        }

        #endregion

        #region Utility methods

        public static DialogResult ShowBox(string txtMessage, string txtTitle, MessageBox_Type BoxType, MessageBox_ButtonType BoxBtnType, MessageBox_ButtonAlign BoxBtnAlign)
        {
            newMessageBox = new BDSMessageBox(txtMessage, txtTitle);           
            newMessageBox.MessageBoxType = BoxType;
            newMessageBox.btnCancel.Focus();
            newMessageBox.MessageBoxButtonType = BoxBtnType;
            newMessageBox.MessageBoxButtonAlign = BoxBtnAlign;           
            DialogResult drResult = newMessageBox.ShowDialog();
            newMessageBox.Dispose();
            return drResult;
        }

        public static DialogResult ShowBox(string txtMessage, string txtTitle,string yesStr , string noStr, MessageBox_Type BoxType, MessageBox_ButtonType BoxBtnType, MessageBox_ButtonAlign BoxBtnAlign)
        {
            newMessageBox = new BDSMessageBox(txtMessage, txtTitle,yesStr , noStr);
            newMessageBox.MessageBoxType = BoxType;
            newMessageBox.btnCancel.Focus();
            newMessageBox.MessageBoxButtonType = BoxBtnType;
            newMessageBox.MessageBoxButtonAlign = BoxBtnAlign;
            DialogResult drResult = newMessageBox.ShowDialog();
            newMessageBox.Dispose();
            return drResult;
        }

        #endregion

        #region private events

        private void btnCancel_MouseHover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES_p;
        }

        private void btnCancel_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES;
        }

        private void btnOK_MouseHover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES_p;
        }

        private void btnOK_MouseLeave(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES;
        }
        #endregion

    }
}
It may helpfull.
Malik M.Shahid  Wednesday, September 09, 2009 8:31 AM
Hi Karthik.sr,

Surely u have been inserting the new dialog box, with the following property start Position center screen , top most true and use the above code as code class.

Good Luck.


Malik M.Shahid  Wednesday, September 09, 2009 11:08 AM
Create custom message box, i think it may resolve your problem,the message box that i am using in my application is given below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace BDSUtilityService
{
    public partial class BDSMessageBox : Form
    {
        static BDSMessageBox newMessageBox;

        string YesStr = "&Yes       ";
        string NoStr =  "&No       ";
        #region public enumeration
        //Enum MessageBox
        public enum MessageBox_Type
        {
            INFORMATIONBOX,
            WARRNINGBOX,
            ERRORBOX,
            QUESTIONBOX
        }
        private MessageBox_Type m_Type = MessageBox_Type.INFORMATIONBOX;

        //Enum Buttons
        public enum MessageBox_ButtonType
        {
            OK,
            CANCEL,
            BOTH
        }
        private MessageBox_ButtonType m_Button = MessageBox_ButtonType.BOTH;

        //Box Button Alignment
        public enum MessageBox_ButtonAlign
        {
            RIGHTLEFT,
            LEFT,
            RIGHT,
            CENTER
        }
        private MessageBox_ButtonAlign m_Align = MessageBox_ButtonAlign.CENTER;
        #endregion

        #region constructors
        public BDSMessageBox()
        {
            InitializeComponent();
            btnOK.Focus();
        }

        public BDSMessageBox(string strMsg,string strCaption)
        {
            InitializeComponent();
            this.lblMessage.Text = strMsg;
            this.lblTitle.Text  = strCaption;
            this.lblMessage.SelectionStart = strMsg.Length;
        }

        public BDSMessageBox(string strMsg, string strCaption, string strYes , string strNo )
        {
            InitializeComponent();
            this.lblMessage.Text = strMsg;
            this.lblTitle.Text = strCaption;
            this.lblMessage.SelectionStart = strMsg.Length;
            YesStr = strYes;
            NoStr = strNo;
        }
        #endregion

        #region MessageBox Properties

        //Box Type
        [Description("Sets the Type of Message Box"),
         Category("Appearance"),
         DefaultValue(0)]
        public MessageBox_Type MessageBoxType
        {
            get { return m_Type; }
            set
            {

                m_Type = value;
                switch (m_Type)
                {
                    case MessageBox_Type.INFORMATIONBOX:
                        pbImage.Image = UtilityResources.INFORMATION;
                        break;
                    case MessageBox_Type.WARRNINGBOX:
                        pbImage.Image = UtilityResources.WARRNING;
                        break;
                    case MessageBox_Type.ERRORBOX:
                        pbImage.Image = UtilityResources.ERROR;
                        break;                   
                    default:
                        pbImage.Image = UtilityResources.QUESTION;
                        btnOK.Text = YesStr; // "&Yes      ";
                        btnCancel.Text = NoStr;//"&No       ";
                        break;
                }

            }
        }

        //Button Alignment
        [Description("Sets the Alignment of Buttons"),
         Category("Appearance"),
         DefaultValue(0)]
        public MessageBox_ButtonAlign MessageBoxButtonAlign
        {
            get { return m_Align; }
            set
            {

                m_Align = value;
                switch (m_Align)
                {
                    case MessageBox_ButtonAlign.RIGHTLEFT:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(12, 140);
                                btnCancel.Location = new Point(463, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(239, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(463, 140);
                                break;
                        }
                        break;
                    case MessageBox_ButtonAlign.RIGHT:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(375, 140);
                                btnCancel.Location = new Point(463, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(463, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(463, 140);
                                break;
                        }
                        break;
                    case MessageBox_ButtonAlign.LEFT:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(12, 140);
                                btnCancel.Location = new Point(101, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(12, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(12, 140);
                                break;
                        }
                        break;

                    default:
                        switch (m_Button)
                        {
                            case MessageBox_ButtonType.BOTH:
                                btnOK.Location = new Point(204, 140);
                                btnCancel.Location = new Point(295, 140);
                                break;
                            case MessageBox_ButtonType.OK:
                                btnOK.Location = new Point(239, 140);
                                break;
                            default:
                                btnCancel.Location = new Point(239, 140);
                                break;
                        }
                        break;
                }

            }
        }

        //MessageBox Buttons Type
        [Description("Sets the type of Buttons"),
         Category("Appearance"),
         DefaultValue(0)]
        public MessageBox_ButtonType MessageBoxButtonType
        {
            get { return m_Button; }
            set
            {

                m_Button = value;
                switch (m_Button)
                {
                    case MessageBox_ButtonType.BOTH:
                        btnCancel.Visible = true;
                        btnOK.Visible = true;
                        break;
                    case MessageBox_ButtonType.CANCEL:
                        btnOK.Visible = false;
                        break;
                    default:
                        btnCancel.Visible = false;
                        break;
                }
            }
        }

        #endregion

        #region Utility methods

        public static DialogResult ShowBox(string txtMessage, string txtTitle, MessageBox_Type BoxType, MessageBox_ButtonType BoxBtnType, MessageBox_ButtonAlign BoxBtnAlign)
        {
            newMessageBox = new BDSMessageBox(txtMessage, txtTitle);           
            newMessageBox.MessageBoxType = BoxType;
            newMessageBox.btnCancel.Focus();
            newMessageBox.MessageBoxButtonType = BoxBtnType;
            newMessageBox.MessageBoxButtonAlign = BoxBtnAlign;           
            DialogResult drResult = newMessageBox.ShowDialog();
            newMessageBox.Dispose();
            return drResult;
        }

        public static DialogResult ShowBox(string txtMessage, string txtTitle,string yesStr , string noStr, MessageBox_Type BoxType, MessageBox_ButtonType BoxBtnType, MessageBox_ButtonAlign BoxBtnAlign)
        {
            newMessageBox = new BDSMessageBox(txtMessage, txtTitle,yesStr , noStr);
            newMessageBox.MessageBoxType = BoxType;
            newMessageBox.btnCancel.Focus();
            newMessageBox.MessageBoxButtonType = BoxBtnType;
            newMessageBox.MessageBoxButtonAlign = BoxBtnAlign;
            DialogResult drResult = newMessageBox.ShowDialog();
            newMessageBox.Dispose();
            return drResult;
        }

        #endregion

        #region private events

        private void btnCancel_MouseHover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES_p;
        }

        private void btnCancel_MouseLeave(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES;
        }

        private void btnOK_MouseHover(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES_p;
        }

        private void btnOK_MouseLeave(object sender, EventArgs e)
        {

            Button btn = (Button)sender;
            btn.BackgroundImage = UtilityResources.button_nav_mini_YES;
        }
        #endregion

    }
}
It may helpfull.
Malik M.Shahid  Wednesday, September 09, 2009 8:31 AM
Hi Malik M.Shahid ,

what is
UtilityResources supposed to do??  Any special functionalty in that class ??

karthik.sr  Wednesday, September 09, 2009 9:04 AM
UtilityResouces it is resource file contains different type of Icons that dispay on dialog box like Question Mark, Information , Warning
Malik M.Shahid  Wednesday, September 09, 2009 9:25 AM
Hi Malik M.Shahid,

I tested with this code, but as per my requirement the message box is not forcing the user to respond to it.
Am i missing something ??? Thanks.
karthik.sr  Wednesday, September 09, 2009 9:53 AM
Hi Karthik.sr,

Surely u have been inserting the new dialog box, with the following property start Position center screen , top most true and use the above code as code class.

Good Luck.


Malik M.Shahid  Wednesday, September 09, 2009 11:08 AM

You can use google to search for other answers

Custom Search

More Threads

• Rightclick meny??
• Form.Hide() not working C# Windows form
• [ToolBoxBitmap... for my custom control
• Communication from child to parent using Events/Delegates?
• Code conversion help (C# .NETto VB .NET)
• Multithreading in c# windows application
• Crystal Reports
• passing a parameter name to a "WITH /end WIth"
• Forcing blue ToolStrip/menu on Vista
• Question about opening a USB device handle