?
Solved

DialogResult from a MessageBox.Show

Posted on 2006-11-25
9
Medium Priority
?
734 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
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 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 63

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 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

801 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