Implementing Yes/No MessageBox with DialogResult in C# WinForms

Oct 29, 2025 · Programming · 29 views · 7.8

Keywords: C# | WinForms | MessageBox | DialogResult | Message Box

Abstract: This article provides a comprehensive guide to creating Yes/No message boxes in C# WinForms applications and properly retrieving DialogResult values. It explores various overloads of the MessageBox.Show method, demonstrates DialogResult enumeration usage, and offers complete code examples with best practices. The content also compares standard message boxes with custom dialog implementations to help developers choose the most appropriate solution for their specific requirements.

Basic Usage of MessageBox.Show Method

In C# WinForms development, the MessageBox class provides static methods for displaying message boxes without requiring instance creation. The MessageBox.Show method offers multiple overloads supporting different parameter combinations to customize the message box appearance and behavior.

DialogResult Enumeration and Button Configuration

The DialogResult enumeration defines possible return values from dialog boxes, including Yes, No, OK, Cancel, Abort, Retry, Ignore, and others. When configuring message box buttons with MessageBoxButtons.YesNo, user selections return corresponding DialogResult values.

// Basic Yes/No message box implementation
DialogResult result = MessageBox.Show(
    "Are you sure you want to perform this action?", 
    "Confirmation Dialog", 
    MessageBoxButtons.YesNo
);

if (result == DialogResult.Yes)
{
    // Handle "Yes" selection
    Console.WriteLine("User confirmed the action");
}
else if (result == DialogResult.No)
{
    // Handle "No" selection
    Console.WriteLine("User cancelled the action");
}

Advanced Message Box Configuration

Beyond basic text and button configuration, MessageBox.Show supports advanced features including icons, default buttons, and options. The MessageBoxIcon enumeration provides various standard icons such as Question, Information, Warning, and Error to enhance user experience.

// Message box with icon and default button
DialogResult result = MessageBox.Show(
    "Do you want to save changes to the document?",
    "Document Save",
    MessageBoxButtons.YesNoCancel,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button1
);

switch (result)
{
    case DialogResult.Yes:
        // Save document
        SaveDocument();
        break;
    case DialogResult.No:
        // Close without saving
        CloseDocument();
        break;
    case DialogResult.Cancel:
        // Cancel operation
        break;
}

Custom Dialog Implementation

When standard message boxes cannot meet complex requirements, custom dialogs can be created. Custom dialogs offer greater flexibility, allowing inclusion of arbitrary controls and complex business logic.

public static class CustomMessageBox
{
    public static DialogResult ShowCustomDialog(string message, string title)
    {
        Form dialog = new Form()
        {
            Width = 300,
            Height = 150,
            FormBorderStyle = FormBorderStyle.FixedDialog,
            Text = title,
            StartPosition = FormStartPosition.CenterParent,
            MaximizeBox = false,
            MinimizeBox = false
        };

        Label messageLabel = new Label()
        {
            Text = message,
            Left = 20,
            Top = 20,
            Width = 260,
            Height = 40
        };

        Button yesButton = new Button()
        {
            Text = "Yes",
            Left = 50,
            Top = 70,
            Width = 80,
            DialogResult = DialogResult.Yes
        };

        Button noButton = new Button()
        {
            Text = "No", 
            Left = 150,
            Top = 70,
            Width = 80,
            DialogResult = DialogResult.No
        };

        dialog.Controls.Add(messageLabel);
        dialog.Controls.Add(yesButton);
        dialog.Controls.Add(noButton);
        
        dialog.AcceptButton = yesButton;
        dialog.CancelButton = noButton;

        return dialog.ShowDialog();
    }
}

Practical Application Scenarios

Yes/No message boxes are particularly useful in form closing confirmation scenarios. By displaying confirmation dialogs in the FormClosing event, accidental closure of windows containing unsaved data can be prevented.

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (HasUnsavedChanges())
    {
        DialogResult result = MessageBox.Show(
            "The document contains unsaved changes. Are you sure you want to exit?",
            "Confirm Exit",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Warning
        );

        if (result == DialogResult.No)
        {
            e.Cancel = true; // Cancel closing operation
        }
    }
}

Performance and Best Practices

MessageBox.Show is a modal dialog that blocks the current thread until user response. In asynchronous programming scenarios, alternative approaches should be considered to avoid UI freezing. For frequently used confirmation dialogs, creating reusable custom components is recommended.

Cross-Platform Considerations

While this article primarily discusses the WinForms platform, similar dialog concepts exist in other GUI frameworks. WPF provides the MessageBox class, ASP.NET uses JavaScript's confirm function, and console applications require custom text-based interaction implementations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.