Introduction
In this article, I am going to explain how to create a custom message box in C#.NET. As we know, we can use a MessageBox
class to display a message box, but that does not look beautiful. When we make a project, sometimes we need to create a custom message box because ordinary message box does not fill our requirements such as MessageBox
which has check box control like Do Not Show Next time or has a gradient background color, etc. In this demo, I will tell you how to create a custom message box which has a gradient background color. For completing this demo, I used Visual Studio 2010 as IDE and C# as programming language.
In this demo, I created two classes, frmShowMessage
which displays message box and frmMain
class which is used to test application. Then I created two enumerations, one is enumMessageIcon
which contains Icon for Image and the other one is enumMessageButton
which contains type of buttons to be displayed.
frmShowMessage
class has a static
overloaded method named Show()
which is used to display message. First type accepts one string
parameter which is message needs to be displayed, second type accepts two string
parameters, first is message to be displayed and second one is message title and the last one is accept four parameters, first one is messageText
, second one is messageTitle
, third one is messageIcon
and last one is messageButton
. You can create more overloaded methods according to your need. Let’s start with this demo.
First, create a form named frmShowMessage
and write down the following lines of code. I had used ImageList
control which contains all images to be displayed on frmShowMessage
and creates two enumerations in the same namespace.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace MessageBoxDemo
{
public partial class frmShowMessage : Form
{
public frmShowMessage()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = this.ClientRectangle;
LinearGradientBrush brush = new LinearGradientBrush
(rect, Color.SkyBlue, Color.AliceBlue, 60);
e.Graphics.FillRectangle(brush, rect);
base.OnPaint(e);
}
private void setMessage(string messageText)
{
int number = Math.Abs(messageText.Length / 30);
if (number != 0)
this.lblMessageText.Height = number * 25;
this.lblMessageText.Text = messageText;
}
private void addButton(enumMessageButton MessageButton)
{
switch (MessageButton)
{
case enumMessageButton.OK:
{
Button btnOk = new Button();
btnOk.Text = "OK";
btnOk.DialogResult = DialogResult.OK;
btnOk.FlatStyle = FlatStyle.Popup;
btnOk.FlatAppearance.BorderSize = 0;
btnOk.SetBounds(pnlShowMessage.ClientSize.Width - 80,
5, 75, 25);
pnlShowMessage.Controls.Add(btnOk);
}
break;
case enumMessageButton.OKCancel:
{
Button btnOk = new Button();
btnOk.Text = "OK";
btnOk.DialogResult = DialogResult.OK;
btnOk.FlatStyle = FlatStyle.Popup;
btnOk.FlatAppearance.BorderSize = 0;
btnOk.SetBounds((pnlShowMessage.ClientSize.Width - 70),
5, 65, 25);
pnlShowMessage.Controls.Add(btnOk);
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.DialogResult = DialogResult.Cancel;
btnCancel.FlatStyle = FlatStyle.Popup;
btnCancel.FlatAppearance.BorderSize = 0;
btnCancel.SetBounds((pnlShowMessage.ClientSize.Width -
(btnOk.ClientSize.Width + 5 + 80)), 5, 75, 25);
pnlShowMessage.Controls.Add(btnCancel);
}
break;
case enumMessageButton.YesNo:
{
Button btnNo = new Button();
btnNo.Text = "No";
btnNo.DialogResult = DialogResult.No;
btnNo.FlatStyle = FlatStyle.Popup;
btnNo.FlatAppearance.BorderSize = 0;
btnNo.SetBounds((pnlShowMessage.ClientSize.Width - 70),
5, 65, 25);
pnlShowMessage.Controls.Add(btnNo);
Button btnYes = new Button();
btnYes.Text = "Yes";
btnYes.DialogResult = DialogResult.Yes;
btnYes.FlatStyle = FlatStyle.Popup;
btnYes.FlatAppearance.BorderSize = 0;
btnYes.SetBounds((pnlShowMessage.ClientSize.Width -
(btnNo.ClientSize.Width + 5 + 80)), 5, 75, 25);
pnlShowMessage.Controls.Add(btnYes);
}
break;
case enumMessageButton.YesNoCancel:
{
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.DialogResult = DialogResult.Cancel;
btnCancel.FlatStyle = FlatStyle.Popup;
btnCancel.FlatAppearance.BorderSize = 0;
btnCancel.SetBounds
((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25);
pnlShowMessage.Controls.Add(btnCancel);
Button btnNo = new Button();
btnNo.Text = "No";
btnNo.DialogResult = DialogResult.No;
btnNo.FlatStyle = FlatStyle.Popup;
btnNo.FlatAppearance.BorderSize = 0;
btnNo.SetBounds((pnlShowMessage.ClientSize.Width -
(btnCancel.ClientSize.Width + 5 + 80)), 5, 75, 25);
pnlShowMessage.Controls.Add(btnNo);
Button btnYes = new Button();
btnYes.Text = "Yes";
btnYes.DialogResult = DialogResult.No;
btnYes.FlatStyle = FlatStyle.Popup;
btnYes.FlatAppearance.BorderSize = 0;
btnYes.SetBounds((pnlShowMessage.ClientSize.Width -
(btnCancel.ClientSize.Width + btnNo.ClientSize.Width +
10 + 80)), 5, 75, 25);
pnlShowMessage.Controls.Add(btnYes);
}
break;
}
}
private void addIconImage(enumMessageIcon MessageIcon)
{
switch (MessageIcon)
{
case enumMessageIcon.Error:
pictureBox1.Image = imageList1.Images["Error"];
break;
case enumMessageIcon.Information:
pictureBox1.Image = imageList1.Images["Information"];
break;
case enumMessageIcon.Question:
pictureBox1.Image = imageList1.Images["Question"];
break;
case enumMessageIcon.Warning:
pictureBox1.Image = imageList1.Images["Warning"];
break;
}
}
#region Overloaded Show message to display message box.
internal static void Show(string messageText)
{
frmShowMessage frmMessage = new frmShowMessage();
frmMessage.setMessage(messageText);
frmMessage.addIconImage(enumMessageIcon.Information);
frmMessage.addButton(enumMessageButton.OK);
frmMessage.ShowDialog();
}
internal static void Show(string messageText, string messageTitle)
{
frmShowMessage frmMessage = new frmShowMessage();
frmMessage.Text = messageTitle;
frmMessage.setMessage(messageText);
frmMessage.addIconImage(enumMessageIcon.Information);
frmMessage.addButton(enumMessageButton.OK);
frmMessage.ShowDialog();
}
internal static void Show(string messageText, string messageTitle,
enumMessageIcon messageIcon, enumMessageButton messageButton)
{
frmShowMessage frmMessage = new frmShowMessage();
frmMessage.setMessage(messageText);
frmMessage.Text = messageTitle;
frmMessage.addIconImage(messageIcon);
frmMessage.addButton(messageButton);
frmMessage.ShowDialog();
}
#endregion
}
#region constant defined in form of enumeration which is used in showMessage class.
internal enum enumMessageIcon
{
Error,
Warning,
Information,
Question,
}
internal enum enumMessageButton
{
OK,
YesNo,
YesNoCancel,
OKCancel
}
#endregion
}
Now create a frmMain
form which is your main form or we can say start up form and write down the following code to test it:
using System;
using System.Windows.Forms;
namespace MessageBoxDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frmShowMessage.Show("This is message box which represent message with title,
custom button and custom icon.", "This is message title",
enumMessageIcon.Question, enumMessageButton.OKCancel);
}
}
}
After creating these two classes and writing code, build your program and see the output which is as follows:
Please write your valuable comments. Thanks.
History
- 7th January, 2012: Initial version
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.