Hi
How to create a custom message box in Windows Form Application.
Thanks
Hi
How to create a custom message box in Windows Form Application.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jithu ThomasRank #437 · 5.7K reputationPosted Aug 15, 2024, 12:03 AM EDT
Step 1: Create a New Form
Add a New Form: In your Windows Forms project, add a new form to the project. You can do this by right-clicking on your project in the Solution Explorer, selecting "Add," and then "Windows Form." Name it something like
CustomMessageBox.Design the Form:
FormBorderStyletoFixedDialog,MaximizeBoxtoFalse,MinimizeBoxtoFalse, andStartPositiontoCenterParent.Labelto display the message.Buttoncontrols for user actions (e.g., "OK", "Cancel").Iconto represent the type of message (information, warning, error).Step 2: Customize the Form Logic
Add Properties: Add properties to the form to allow passing in the message text and button labels.
public partial class CustomMessageBox : Form { public CustomMessageBox(string message, string title, string button1Text = "OK", string button2Text = "") { InitializeComponent(); this.Text = title; lblMessage.Text = message; btnOK.Text = button1Text; if (!string.IsNullOrEmpty(button2Text)) { btnCancel.Text = button2Text; btnCancel.Visible = true; } else { btnCancel.Visible = false; } } }Button Click Events: Implement the click events for the buttons.
private void btnOK_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); }Step 3: Use the Custom Message Box
private void ShowCustomMessageBox() { using (CustomMessageBox customMessageBox = new CustomMessageBox("Do you want to save changes?", "Confirm Save", "Yes", "No")) { DialogResult result = customMessageBox.ShowDialog(); if (result == DialogResult.OK) { // Handle OK action MessageBox.Show("You clicked Yes!"); } else if (result == DialogResult.Cancel) { // Handle Cancel action MessageBox.Show("You clicked No!"); } } }this is just a basic example try to change accordingly with your logic.
Local Coders197 reputationPosted Aug 15, 2024, 12:07 AM EDT
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); }