Solved

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

Posted on 2007-11-19
1
80,568 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2 questions 10 29
Calculate number of nights between two dates 5 53
Where does legacy ASP.NET initialize language? 2 26
How to repeat the data 4 18
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

828 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