0

Cannot seem to make the message box to not appear when I click cancel. Help please !

        Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
            Dim input As String
            Dim total As Single
            Dim average As Single
            Dim count As Single

            Const msg1 As String = "Enter a sales amount. Click Cancel to end"
            Const msg2 As String = "Sales Entry"


            count = 1

            Do
                input = InputBox(msg1, msg2)


                If Not IsNumeric(input) Then
                    MessageBox.Show("Error, please check input")
                Else
                    count = count + 1
                    total = total + input

                End If



            Loop Until (IsNumeric(input) And count > 5)




            lblTotalSales.Text = total.ToString("C2")

            average = total / 5
            lblAverage.Text = average.ToString("C2")




        End Sub
2
Contributors
1
Reply
27
Views
2 Years
Discussion Span
Last Post by Shark_1
1

There is another control flow needed before

If Not IsNumeric(input) Then

Your codes are quite right and you need some modification. Like

        Do

            input = InputBox(msg1, msg2)

            'Check is the variable stores any value or not
            If Not String.IsNullOrWhiteSpace(input) Then

                'If it stores something do the calculations
                If Not IsNumeric(input) Then
                    MessageBox.Show("Error, please check input")
                Else
                    count = count + 1
                    total = total + input
                End If
            Else
                'If variable input does not contain any value
                'Prompt to user, he is ready to continue or not
                Select Case MessageBox.Show("Do you sure to continue?", "Confirmation.....", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                    Case Windows.Forms.DialogResult.Yes
                        'If the user confirms to continue
                        'then nothing to do
                        Continue Do

                    Case Windows.Forms.DialogResult.No
                        'If the user desires to exit then exit from loop
                        Exit Do

                End Select
            End If
        Loop Until (IsNumeric(input) And count > 5)

An InputBox() always returns a Null Or Empty string when you click the cancel Button. So you must have to consider to make an extracontrol flow for empty string.

Edited by Shark_1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.