Solved

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

Posted on 2007-11-19
1
80,893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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 is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

738 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