none
I am trying to create a message box with yes or no options. RRS feed

  • Question

  • I am trying to create a message box with yes or no options. I am not very familiar with Visual Basic or Visual Studio I am just using it for a computer class This is my code: 

       Private Sub btn_submit_Click(sender As Object, e As EventArgs) Handles btn_submit.Click

            Dim drink As String

            Dim tax As Double

            Dim teatax As Double

            Dim coffee As Double

            Dim soda As Double

            Dim coffeetax As Double

            Dim totalcoffee As double

            Dim sodatax As Double

            Dim totalsoda As Double

            Dim tea As Double

            Dim taxtea As Double

            Dim totaltea As Double

            drink = txt_coffeeTeaSoda.Text

            tax = 0.05

            teatax = 0.07

            coffee = 2.0

            soda = 2.6

            coffeetax = coffee * tax

            totalcoffee = coffeetax + coffee 

            sodatax = soda * tax

            totalsoda = sodatax + soda

            tea = 3.0

            taxtea = tea * teatax

            totaltea = taxtea + tea

            Dim result As DialogResult

            If drink = "coffee" Then
                MessageBox.Show("The coffee we sell is $" & coffee & ".00 when you buy it to go. If you are dining in, there is a sale's tax of 5% so the total will come to $" & totalcoffee & "0")
            ElseIf drink = "soda" Then
                MessageBox.Show("The soda we sell is $" & soda & "0 when you buy it to go. If you are dining in, there is a sale's tax of 5% so the total will come to $" & totalsoda)
            ElseIf drink = "tea" Then
                result = MessageBox.Show("The tea has a higher tax rate than the soda and coffee. The rate is 7%. Would you still like to buy a cup?", MessageBoxButtons.YesNo,)
                If result = DialogResult.Yes Then
                    MessageBox.Show("Awesome! the tea will come to $" & tea & ".00 when you buy it to go. The dine-in price is $" & totaltea)
                ElseIf result = DialogResult.No Then
                    MessageBox.Show("Well..... GO TO MCDONALDS OR SOMETHING YOU ENTITLED STINGY CONSUMER. WHAT YOU EXPECT ME TO DO? CALL THE CORPORATE OFFICE MYSELF AND TELL THEM TO LOWER THEIR PRICES SO YOU CAN HAVE YOUR 20 CENTS? NAH. DON'T LET THE DOOR HIT YOU WHERE THE GOOD LORD SPLIT YOU HONEY")
                End If
            End If
        End Sub
    End Class

    It is saying this

    Error BC30518 Overload resolution failed because no accessible 'Show' can be called with these arguments:
        'Public Shared Overloads Function Show(text As String, caption As String, buttons As MessageBoxButtons) As DialogResult': Argument not specified for parameter 'buttons'.
        'Public Shared Overloads Function Show(owner As IWin32Window, text As String, caption As String) As DialogResult': Argument not specified for parameter 'caption'. AssignmentFive C:\Users\creat\source\repos\AssignmentFive\AssignmentFive\Form1.vb 59 Active

    Thursday, March 15, 2018 7:25 PM

Answers

  • Well you didn't display the code you used which caused the error. And when you display code you should copy it then paste it into a code block so it is legible rather than pasting it as text of your post. Code block is accessed in the menu strip at the top of a post you are editing to the right of the letters HTML. Once you paste code into it, preview the code then post it. The code block will appear at the last position your cursor was in on your post before you selected Insert Code Block.

    Apparently the format you used for the MessageBox was invalid parameter wise as you received an error to that extent.

    MessageBox.Show(Text, Caption, MessageBox buttons to use)

    or

    MessageBox.Show(Text, Caption, MessageBox buttons to use, Icon to use if any)

    Update: Try the below code within the Button Click event sub.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = MessageBox.Show("This is the Text", "This is the caption or title", MessageBoxButtons.YesNo)
            MessageBox.Show(result.ToString)
        End Sub


    La vida loca

    Thursday, March 15, 2018 9:03 PM

All replies

  • There are several variants of MessageBox.Show:

    Try this one:

    result = MessageBox.Show("The tea has a higher tax rate than the soda and coffee. The rate is 7%. Would you still like to buy a cup?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

    • Proposed as answer by Mr. Monkeyboy Thursday, March 15, 2018 9:10 PM
    Thursday, March 15, 2018 7:38 PM
  • Hi

    EDIT: sorry Viorel - I only saw your reply after posting mine.

    Here is one example.

    	Dim tea As Double = 40.5
    	Dim totaltea As Double = 65.75
    
    	Dim result As DialogResult = MessageBox.Show("The tea has a higher tax rate than the soda and coffee. The rate is 7%. Would you still like to buy a cup?", "Are you sure?", MessageBoxButtons.YesNo)
    	If result = DialogResult.Yes Then
    	  MessageBox.Show("Awesome! the tea will come to " & tea.ToString("$0.00") & " When you buy it to go. The dine-In price is " & totaltea.ToString("$0.00"))
    	ElseIf result = DialogResult.No Then
    	  MessageBox.Show("Well..... GO TO MCDONALDS OR SOMETHING YOU ENTITLED STINGY CONSUMER. WHAT YOU EXPECT ME TO DO? CALL THE CORPORATE OFFICE MYSELF AND TELL THEM TO LOWER THEIR PRICES SO YOU CAN HAVE YOUR 20 CENTS? NAH. DON'T LET THE DOOR HIT YOU AS YOU LEAVE!")
    	End If


    Regards Les, Livingston, Scotland



    • Edited by leshay Thursday, March 15, 2018 7:46 PM
    • Proposed as answer by Mr. Monkeyboy Thursday, March 15, 2018 9:10 PM
    Thursday, March 15, 2018 7:44 PM
  • I had actually tried this one will no luck either
    Thursday, March 15, 2018 8:19 PM
  • Leshay, I don't understand how this would solve my error message. I think you just showed me a better way to use the $ sign. I know that the way I have coded $ it is unconventional, but it works. What I am having trouble with is displaying the yes or no messagebox within another nested if statement. Just to be sure, I tried your code. It is still giving me the same message:

    Error BC30518 Overload resolution failed because no accessible 'Show' can be called with these arguments:
        'Public Shared Overloads Function Show(text As String, caption As String, buttons As MessageBoxButtons) As DialogResult': Argument not specified for parameter 'buttons'.
        'Public Shared Overloads Function Show(owner As IWin32Window, text As String, caption As String) As DialogResult': Argument not specified for parameter 'caption'. AssignmentFive C:\Users\creat\source\repos\AssignmentFive\AssignmentFive\Form1.vb 59 Active

    Thursday, March 15, 2018 8:29 PM
  • Well you didn't display the code you used which caused the error. And when you display code you should copy it then paste it into a code block so it is legible rather than pasting it as text of your post. Code block is accessed in the menu strip at the top of a post you are editing to the right of the letters HTML. Once you paste code into it, preview the code then post it. The code block will appear at the last position your cursor was in on your post before you selected Insert Code Block.

    Apparently the format you used for the MessageBox was invalid parameter wise as you received an error to that extent.

    MessageBox.Show(Text, Caption, MessageBox buttons to use)

    or

    MessageBox.Show(Text, Caption, MessageBox buttons to use, Icon to use if any)

    Update: Try the below code within the Button Click event sub.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = MessageBox.Show("This is the Text", "This is the caption or title", MessageBoxButtons.YesNo)
            MessageBox.Show(result.ToString)
        End Sub


    La vida loca

    Thursday, March 15, 2018 9:03 PM
  • Public Class Form1
        Private Sub btn_submit_Click(sender As Object, e As EventArgs) Handles btn_submit.Click
    
            Dim drink As String
    
            Dim tax As Double
    
            Dim teatax As Double
    
            Dim coffee As Double
    
            Dim soda As Double
    
            Dim coffeetax As Double
    
            Dim totalcoffee As double
    
            Dim sodatax As Double
    
            Dim totalsoda As Double
    
            Dim tea As Double
    
            Dim taxtea As Double
    
            Dim totaltea As Double
    
            drink = txt_coffeeTeaSoda.Text
    
            tax = 0.05
    
            teatax = 0.07
    
            coffee = 2.0
    
            soda = 2.6
    
            coffeetax = coffee * tax
    
            totalcoffee = coffeetax + coffee 
    
            sodatax = soda * tax
    
            totalsoda = sodatax + soda
    
            tea = 3.0
    
            taxtea = tea * teatax
    
            totaltea = taxtea + tea
    
            Dim result As DialogResult
    
            If drink = "coffee" Then
                MessageBox.Show("The coffee we sell is $" & coffee & ".00 when you buy it to go. If you are dining in, there is a sale's tax of 5% so the total will come to $" & totalcoffee & "0")
            ElseIf drink = "soda" Then
                MessageBox.Show("The soda we sell is $" & soda & "0 when you buy it to go. If you are dining in, there is a sale's tax of 5% so the total will come to $" & totalsoda)
            ElseIf drink = "tea" Then
                MessageBox.Show("The tea has a higher tax rate than the soda and coffee. The rate is 7%. Would you still like to buy a cup?", MessageBoxButtons.YesNo,)
                If result = DialogResult.Yes Then
                    MessageBox.Show("Awesome! the tea will come to $" & tea & ".00 when you buy it to go. The dine-in price is $" & totaltea)
                ElseIf result = DialogResult.No Then
                    MessageBox.Show("Well..... GO TO MCDONALDS OR SOMETHING YOU ENTITLED STINGY CONSUMER. WHAT YOU EXPECT ME TO DO? CALL THE CORPORATE OFFICE MYSELF AND TELL THEM TO LOWER THEIR PRICES SO YOU CAN HAVE YOUR 20 CENTS? NAH. DON'T LET THE DOOR HIT YOU WHERE THE GOOD LORD SPLIT YOU")
                End If
            End If
        End Sub
    End Class

    This is the part I am getting the error message for:

     MessageBox.Show("The tea has a higher tax rate than the soda and coffee. The rate is 7%. Would you still like to buy a cup?", MessageBoxButtons.YesNo,)
                If result = DialogResult.Yes Then


    Thursday, March 15, 2018 9:15 PM
  • I did not know there was a code box, this is my first post.
    Thursday, March 15, 2018 9:17 PM
  • I did not know there was a code box, this is my first post.

    No worries

    Maybe the below code. You didn't provide a caption, which can just be an empty string i.e. two sets of double quotes with nothing between them, prior to specifying MessageBox buttons. I provided a string "Tea tax rate". I also altered the code to set the result for the appropriate MessageBox.Show.

    However the original question was answered by the two original answerers really. So you would mark one or both of their posts as the answer at the bottom of their posts where it says Mark as Answer or something to that effect for what you, the thread originator, will see.

            Dim result As DialogResult
    
            If drink = "coffee" Then
                MessageBox.Show("The coffee we sell is $" & coffee & ".00 when you buy it to go. If you are dining in, there is a sale's tax of 5% so the total will come to $" & totalcoffee & "0")
            ElseIf drink = "soda" Then
                 MessageBox.Show("The soda we sell is $" & soda & "0 when you buy it to go. If you are dining in, there is a sale's tax of 5% so the total will come to $" & totalsoda)
            ElseIf drink = "tea" Then
                result = MessageBox.Show("The tea has a higher tax rate than the soda and coffee. The rate is 7%. Would you still like to buy a cup?",  "Tea tax rate", MessageBoxButtons.YesNo,)
                If result = DialogResult.Yes Then
                    MessageBox.Show("Awesome! the tea will come to $" & tea & ".00 when you buy it to go. The dine-in price is $" & totaltea)
                ElseIf result = DialogResult.No Then
                    MessageBox.Show("Well..... GO TO MCDONALDS OR SOMETHING YOU ENTITLED STINGY CONSUMER. WHAT YOU EXPECT ME TO DO? CALL THE CORPORATE OFFICE MYSELF AND TELL THEM TO LOWER THEIR PRICES SO YOU CAN HAVE YOUR 20 CENTS? NAH. DON'T LET THE DOOR HIT YOU WHERE THE GOOD LORD SPLIT YOU")
                End If
            End If


    La vida loca

    Thursday, March 15, 2018 9:33 PM
  • What you have works just fine but if you would like to have code that is easier to read consider the following which adds a class to My Namespace.

    Create MyDialogs code module and insert the following to replace the default contents

    Namespace My
        <ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
        Partial Friend Class _Dialogs
            Public Function Question(ByVal Text As String) As Boolean
                Return (MessageBox.Show(Text, Application.Info.Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
            Public Function Question(ByVal Text As String, ByVal Title As String) As Boolean
                Return (MessageBox.Show(Text, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
            Public Function Question(ByVal Text As String, ByVal Title As String, ByVal DefaultButton As MsgBoxResult) As Boolean
                Dim db As MessageBoxDefaultButton
                If DefaultButton = MsgBoxResult.No Then
                    db = MessageBoxDefaultButton.Button2
                End If
                Return (MessageBox.Show(Text, Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, db) = MsgBoxResult.Yes)
            End Function
        End Class
        <HideModuleName()>
        Friend Module RebeccaMetras_Dialogs
            Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
            ReadOnly Property Dialogs() As _Dialogs
                Get
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace
    

    Use it

    If My.Dialogs.Question("Would you like to continue") Then
        MessageBox.Show("You selected yes")
    Else
        MessageBox.Show("You selected no")
    End If
    Please note that I have changed the default button from Yes to No.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, March 15, 2018 10:04 PM
    Moderator