Solved

DialogResult from a MessageBox.Show

Posted on 2006-11-25
9
727 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

17 Experts available now in Live!

Get 1:1 Help Now