Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DialogResult from a MessageBox.Show

Posted on 2006-11-25
9
Medium Priority
?
735 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 672 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
Industry Leaders: 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 664 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 664 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 64

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

Industry Leaders: 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!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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