Solved

DialogResult from a MessageBox.Show

Posted on 2006-11-25
9
726 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
9 Comments
 
LVL 69

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 69

Expert Comment

by:Éric Moreau
ID: 18014107
gangwisch,

Your ElseIf does not make sense. No optionSelected is initialized.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 62

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 62

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now