How to create a custom message box in C#
By FoxLearn 11/27/2024 2:31:52 PM 52.98K
Why Use Custom Buttons in C# MessageBox?
The default MessageBox
in C# only offers a predefined set of buttons, such as OK, Cancel, Yes, No, etc. Sometimes, your application might need specific buttons with custom labels or actions. Creating a custom message box gives you the flexibility to:
- Design your message box with any number of buttons.
- Use custom button labels that better match your application’s tone.
- Customize the layout and behavior of the buttons.
In this article, we will walk through how to create a custom message box in C# using Windows Forms, providing you with the ability to fully control the appearance and functionality of your message dialogs.
How to customize a message box in C#?
In C#, you can create a custom message box by creating your own form with customized buttons, icons, and message content.
First, 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.
For this example, we'll use two buttons on the main form. You can design your form using the Visual Studio Designer.
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.
You can now start creating your custom message boxes.
In this example, we will create two types of message boxes:
- MessageBox with OK Button
- MessageBox with Yes/No Button
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#.
Create two forms:
frmMessageOK
for displaying a message with an OK button.frmMessageYesNo
for displaying a message with Yes/No buttons.
C# Message box ok
For example, frmMessageOK
is a C# message box with custom buttons.
C# Message box yes no
frmMessageYesNo
C# Custom message box
To make it reusable, we will create a static class MyMessageBox
that handles showing custom message boxes. This class will check which button types (OK or Yes/No) need to be displayed and show the corresponding custom form.
//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; // Check which button configuration to use switch (button) { case System.Windows.Forms.MessageBoxButtons.OK: using (frmMessageOK msgOK = new frmMessageOK()) // c# messagebox custom buttons { // Set the caption, message, and icon based on user input 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; } }
Now, go to your Form1 and add event handlers for the buttons to show the custom message boxes.
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(); } // c# messagebox custom buttons private void button1_Click(object sender, EventArgs e) { // Show the custom message box with an OK button MyMessageBox.ShowMessage("Hello world !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } // c# messagebox custom buttons private void button2_Click(object sender, EventArgs e) { // Show the custom message box with Yes/No buttons if(MyMessageBox.ShowMessage("Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { // Show confirmation of deletion MyMessageBox.ShowMessage("Yes !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } else MyMessageBox.ShowMessage("No !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
In each form, add a Label
control for the message text and a PictureBox
for the icon.
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.
Here’s how you can implement the frmMessageOK
form:
C# Custom dialog box example
frmMessageOK
//c# custom messagebox public partial class frmMessageOK : Form { public frmMessageOK() { InitializeComponent(); } // Property to set and get the message icon public Image MessageIcon { get { return pictureBox.Image; } set { pictureBox.Image = value; } } // Property to set and get the message text public string Message { get { return lblMessage.Text; } set { lblMessage.Text = value; } } }
Similarly, implement the frmMessageYesNo
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, 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.
By following the steps outlined in this article, you can easily create your own message box with custom buttons and enhance the overall user experience of your C# Windows Forms applications.
VIDEO TUTORIAL