C#: How Do I Create A Message Box With “yes”, “no” Choices And A DialogResult?

To create a message box with "Yes" and "No" choices and retrieve the result as a DialogResult, you can make use of the MessageBox class in C#. You can follow the example below:

using System;
using System.Windows.Forms;

class Program
{
    static void Main()
    {
        // Display the message box with "Yes" and "No" choices
        DialogResult result = MessageBox.Show("Do you want to continue?", "Confirmation", MessageBoxButtons.YesNo);

        if (result == DialogResult.Yes)
        {
            // User clicked "Yes"
            Console.WriteLine("You selected Yes.");
        }
        else if (result == DialogResult.No)
        {
            // User clicked "No"
            Console.WriteLine("You selected No.");
        }

        // Wait for user input to exit
        Console.ReadLine();
    }
}

In this example, the MessageBox.Show method displays the message box with the specified message ("Do you want to continue?") and caption ("Confirmation"). The MessageBoxButtons.YesNo parameter ensures that the message box contains "Yes" and "No" choices.

By comparing the returned DialogResult value with DialogResult.Yes or DialogResult.No, you can determine which button the user clicked.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!