Solved

C# using Message.box == DialogResult.Yes and handling the button selection

Posted on 2007-11-19
1
79,882 Views
Last Modified: 2012-06-21
C# VS 2008/2005 - Dianlogue box button selection. Simple.

I have a YES or NO Dialogue box and when the users selects YES or NO they get another dialogue box informing them of their selection. Simple yea? Mine is not working it lets me click the main message box YES or NO twice and then gives me a YES possibly NO. Looked so straight forward.

Thoughts?

        private void btnDisplayMessageBox_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Select yes or No.", "Yes or No", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            if (MessageBox.Show("Select yes or No.", "Yes or No", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes);
            {
                MessageBox.Show("You chose Yes.");
                // I tried break, return and this.Close() here but I do not like the results.
            }
            if (MessageBox.Show("Select yes or No.", "Yes or No", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No);
            {
                MessageBox.Show("You chose No.");
                // I tried break, return and this.Close() here but I do not like the results.
            }
        }
0
Comment
Question by:Karl66
1 Comment
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20314269
Should be like this:
private void btnDisplayMessageBox_Click(object sender, EventArgs e)

        {

            DialogResult result = MessageBox.Show("Select yes or No.", "Yes or No", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
 

            if (result == DialogResult.Yes)

            {

                MessageBox.Show("You chose Yes.");

            } 

            else if (result == DialogResult.No)

            {

                MessageBox.Show("You chose No.");

            }

        }

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now