Message Box Class - Confirm,Error, Warning and Info - C#

A C# Snippet for creating Message Box with various type of message (Confirm,Error, Warning and Info).

Message Box Class

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace messageBox
{
    public static class MsgBox
    {

        // Displays a Confirm type message box    
        // parameter name="sMsg" is The message you want to display
        public static bool Confirm(string sMsg)
        {
            return Confirm("Confirm :", sMsg);
        }
        public static bool Confirm(string sTitle, string sMsg)
        {
            DialogResult ret = MessageBox.Show(sMsg, sTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return (ret == DialogResult.Yes);
        }

        //---------------------------------------------------------------------
        // Displays a Error type message box      
        // parameter name="sMsg" is The message you want to display
        public static void Error(string sMsg)
        {
            Error("Error :", sMsg);
        }
        public static void Error(string sTitle, string sMsg)
        {
            MessageBox.Show(sMsg, sTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        //---------------------------------------------------------------------
        // Displays a Warning type message box      
        // parameter name="sMsg" is The message you want to display
        public static void Warning(string sMsg)
        {
            Warning("", sMsg);
        }

        //---------------------------------------------------------------------

        // Displays a Warning type message box      
        // parameter name="sCaption" is  Name of Application or Class or Method
        // parameter name="sMsg" is The message you want to display
        public static void Warning(string sCaption, string sMsg)
        {
            if (sCaption.Length == 0)
                sCaption = "Warning";
            MessageBox.Show(sMsg, sCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        //---------------------------------------------------------------------
        // Displays a Information type message box      
        // parameter name="sMsg" is The message you want to display
        public static void Info(string sMsg)
        {
            Info("", sMsg);
        }

        //---------------------------------------------------------------------
        // Displays a Information type message box      
        // parameter name="sCaption" is Name of Application or Class or Method
        // parameter name="sMsg" is The message you want to display
        public static void Info(string sCaption, string sMsg)
        {
            if (sCaption.Length == 0)
                sCaption = "Information";
            MessageBox.Show(sMsg, sCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example