Windows Forms: How to Custom a Message Box in C#

This post shows you How to Custom a Message Box in C# .NET Windows Forms Application.

First, Open your Visual Studio, then click New Project. Next, Select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CustomMessageBox" and then click OK button.

c# custom messagebox

Custom message box c#

Dragging two Button controls from the Visual Toolbox, then design your form as shown below.

Form1

custom messagebox in c#

Next, We will create custom message boxes.

For example: MessageBox with OK button and MessageBox with YesNo button. You can do the same for other custom message boxes.

Creating a custom message box allows you to easily increase the size of message box in c#.

C# Message box ok

frmMessageOK

custom message box in c#

C# Message box yes no

frmMessageYesNo

c# custom message box

C# Custom message box

Creating a MyMessageBox class that allows you to show your custom mesagebox.

//custom c# messagebox message
public static class MyMessageBox
{
    public static System.Windows.Forms.DialogResult ShowMessage(string message, string caption, System.Windows.Forms.MessageBoxButtons button, System.Windows.Forms.MessageBoxIcon icon)
    {
        System.Windows.Forms.DialogResult dlgResult = System.Windows.Forms.DialogResult.None;
        switch (button)
        {
            case System.Windows.Forms.MessageBoxButtons.OK:
                using (frmMessageOK msgOK = new frmMessageOK())
                {
                    //Change text, caption, icon
                    msgOK.Text = caption;
                    msgOK.Message = message;
                    switch (icon)
                    {
                        case System.Windows.Forms.MessageBoxIcon.Information:
                            msgOK.MessageIcon = CustomMessageBox.Properties.Resources.Information;
                            break;
                        case System.Windows.Forms.MessageBoxIcon.Question:
                            msgOK.MessageIcon = CustomMessageBox.Properties.Resources.Question;
                            break;
                    }
                    dlgResult = msgOK.ShowDialog();
                }
                break;
            case System.Windows.Forms.MessageBoxButtons.YesNo:
                using (frmMessageYesNo msgYesNo = new frmMessageYesNo())
                {
                    msgYesNo.Text = caption;
                    msgYesNo.Message = message;
                    switch (icon)
                    {
                        case System.Windows.Forms.MessageBoxIcon.Information:
                            msgYesNo.MessageIcon = CustomMessageBox.Properties.Resources.Information;
                            break;
                        case System.Windows.Forms.MessageBoxIcon.Question:
                            msgYesNo.MessageIcon = CustomMessageBox.Properties.Resources.Question;
                            break;
                    }
                    dlgResult = msgYesNo.ShowDialog();
                }
                break;
        }
        return dlgResult;
    }
}

Open your Form1 code behind, then add click event handler to your button controls as shown below

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomMessageBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyMessageBox.ShowMessage("Hello world !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Display your message box
            if(MyMessageBox.ShowMessage("Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                MyMessageBox.ShowMessage("Yes !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MyMessageBox.ShowMessage("No !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

Open your frmMessageOK form, then add MessageIcon and Message properties to your form allows you to set icon and message to your custom message box.

C# Custom dialog box example​

frmMessageOK

//c# custom messagebox
public partial class frmMessageOK : Form
{
    public frmMessageOK()
    {
        InitializeComponent();
    }

    public Image MessageIcon
    {
        get { return pictureBox.Image; }
        set { pictureBox.Image = value; }
    }

    public string Message
    {
        get { return lblMessage.Text; }
        set { lblMessage.Text = value; }
    }
}

Do the same for frmMessageOK form

frmMessageYesNo

//c# custom message box
public partial class frmMessageYesNo : Form
{
    public frmMessageYesNo()
    {
        InitializeComponent();
    }

    public Image MessageIcon
    {
        get { return pictureBox.Image; }
        set { pictureBox.Image = value; }
    }

    public string Message
    {
        get { return lblMessage.Text; }
        set { lblMessage.Text = value; }
    }
}

You can easily increase the size of message box in c# or change messagebox font color in c# by creating a custom message box. This is the best way to make your own message box instead of using the default message box.

VIDEO TUTORIAL