How to customize message box in C#
By FoxLearn 7/18/2024 7:59:57 AM 52.13K
Open your Visual Studio, then click New Project. Next, Select Visual C# on the left, then Windows and then select Windows Forms Application. Enter your project name and then click OK button.
How to Custom message box c#
In C#, you can create a custom message box by creating your own form with customized buttons, icons, and message content.
Drag and drop two Button controls from the Visual Toolbox, then design your form as shown below.
Form1
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
C# Message box yes no
frmMessageYesNo
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) { // c# show message box 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; } } }
In this example, MyMessageBox is a custom form that inherits from Form. You can design this form in the Visual Studio designer just like any other form. The Form1 class demonstrates how to use this custom message box by instantiating it and showing it as a modal dialog.
You can customize the appearance and behavior of your custom message box according to your requirements by adding more functionality to the MyMessageBox class.
You can also easily increase the size of message box in c# or change messagebox font size, 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