Solved

DialogResult from a MessageBox.Show

Posted on 2006-11-25
9
731 Views
Last Modified: 2012-06-27
How do i use the DialogResult (or something similar) from a MessageBox.Show that has a yes/no option on it?  I want to do one thing if the user selects the yes and another thing if no... i'm sure you get the picture.

i tried the following in my    "Private Sub CheckOutMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckOutMenuItem.Click"
 
MessageBox.Show(messagestring, "Check Out", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)


If optionSelected = DialogResult.Yes Then
            totalSales.salesNumberInteger(1) += TotalCost
            'totalSales.salesNumberInteger(0) = 0
        ElseIf optionSelected = DialogResult.No Then
            clearTransaction()
End If

but when i test it and select either yes or no, i don't get any results... it seems like VB.NET transfer control/focus to something else and my "IF" statement isn't executed.  is there another object that should be used with MessageBox.Show?

thanks
0
Comment
Question by:03xc600
[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
9 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 168 total points
ID: 18013370
Hi 03xc600,

You need to store the result:
optionSelected = MessageBox.Show(messagestring, "Check Out", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)


Cheers!
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 18013387
the best way to implement this is:
If MessageBox.Show(messagestring, "Check Out", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            totalSales.salesNumberInteger(1) += TotalCost
            'totalSales.salesNumberInteger(0) = 0
        ElseIf optionSelected = DialogResult.No Then
            clearTransaction()
End If
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 18014107
gangwisch,

Your ElseIf does not make sense. No optionSelected is initialized.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:Obadah_HighTech
Obadah_HighTech earned 166 total points
ID: 18014133
Hello,

The REAL best way as I see it is this :

        Dim optionSelected As DialogResult
        optionSelected = MessageBox.Show(messagestring, "Check Out", MessageBoxButtons.YesNo)
        If optionSelected = Windows.Forms.DialogResult.Yes Then
            totalSales.salesNumberInteger(1) += TotalCost
            'totalSales.salesNumberInteger(0) = 0
        ElseIf optionSelected = Windows.Forms.DialogResult.No Then
            clearTransaction()
        End If
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 166 total points
ID: 18014236
Hello 03xc600;

This is how I would do it. I would also set the MessageBox default selected button. In the sample code below the No button in the MessageBox is selected button. If you want to make the Yes the default button then change this, MessageBoxDefaultButton.Button2 to MessageBoxDefaultButton.Button1.

            Dim messagestring As String = "Message to be displayed"
            Dim optionSelected As DialogResult
            optionSelected = MessageBox.Show(messagestring, "Check Out", _
                MessageBoxButtons.YesNo, MessageBoxIcon.Hand, _
                MessageBoxDefaultButton.Button2)
            If optionSelected = DialogResult.Yes Then
                totalSales.salesNumberInteger(1) += TotalCost
               'totalSales.salesNumberInteger(0) = 0
            Else
                clearTransaction()
            End If


Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 18228285
Hi Bob;

I would like to see a resolution to this question seeming I gave a working solution.

Thank you;

Fernando
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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