Search code examples
vb.netmsgbox

VB.NET - Avoid MsgBox to be executed when a "reset" button is clicked


I just started coding in my college using Visual Basic, and I am still learning how to code.

My question is: When the Click event is handled for a reset button in my Form, all the TextBoxes which say MsgBox("Please insert a value.") is executed. Is there any other way to use these functions without executing those MsgBox? I want to not raise the MsgBoxes when the reset button is clicked.

Here is my code:

Public Class MainWindowApp
    '--------------------------------------------------------------------------------------------------------------------------------
    'Program Info
    '--------------------------------------------------------------------------------------------------------------------------------
    'Program:   Calculate the Average - Windows Forms
    'Date:      10/10/2018
    'Author:    Edino de Souza
    'Operation: Create a VB Form application that will prompt the user for 3 different numbers. 
    '           It should Then calculate the average of those 3 numbers And display the Average To the user On the Console. 
    '           The output message should read As follows
    '           The Average Of Number 1 And Number 2 And Number 3 Is Answer.
    '           Ex. The Average of 90 And 80 And 70 Is 80
    '           2) Using if statements, prevent user input errors from occurring when clicking the Calculate button.
    '           The following should be checked:
    '               a) That a Value was provided for each edit box.
    '               b) That the value input is a number (Note the isNumeric function can be used for this).
    '               c) That the value entered in to the number boxes are not Less than 0 and not greater than 100.
    '           If the value falls under one of the conditions above, use the messagebox function to display a relevant message
    '           to the user (Note, if you want to place the cursor in the textbox being checked automatically, you can call the
    '           focus function of the textbox you are checking)
    '           3) If you are not using a label control to report the result of the calculation, add one to your form. Once the
    '           average has been calculated, set the text of your label control to the average. Using if statements, based on 
    '           the resulting grade, change the text as follows:
    '           60% and above, change the text to green
    '           Below 60%, set the color to red
    '           The forecolor property of the label can be used to change the color.
    '
    '--------------------------------------------------------------------------------------------------------------------------------
    'Change Log
    '--------------------------------------------------------------------------------------------------------------------------------
    'Date               Programmer          Change
    '--------------------------------------------------------------------------------------------------------------------------------
    '10/10/2018         Edino de Souza      Initial/Final Version
    '10/15/2018         Edino de Souza      Adjusted reset button to reset variables too.
    '10/17/2018         Edino de Souza      First part of the addition of the IF statements
    '10/17/2018         Edino de Souza      Adjusted the code to perform what is required.
    '--------------------------------------------------------------------------------------------------------------------------------


    'Begin of Variables Session

    Public num1 As Decimal
    Public num2 As Decimal
    Public num3 As Decimal
    Public CalcAverage As Decimal

    'End of Variables Session


    'Begin of Input Session 1

    Private Sub text_num1_TextChanged(sender As Object, e As EventArgs) Handles text_num1.TextChanged

        'TextBox and Clear button controlling.

        text_num1.Enabled = True    'Enable textbox 1
        Bt_Clear1.Enabled = True    'Enable clear button 1

        text_num2.Enabled = False   'Disable textbox 2
        Bt_Clear2.Enabled = False   'Enable clear button 2

        text_num3.Enabled = False   'Disable textbox 3
        Bt_Clear3.Enabled = False   'Disable button 3

        LabelCalcAverage.Enabled = False    'Disable the calculator button
        Tx_Average.Enabled = False      'Disable the average textbox

        'End - TextBox and Clear button controlling.


        'Section to verify if all the conditions were satisfied.

        If text_num1.Text = Nothing Then
            MsgBox("Please insert a value.")
            text_num2.Enabled = False

        ElseIf IsNumeric(text_num1.Text) = False Then
            MsgBox("Please insert a number")
            text_num2.Enabled = False

        ElseIf (text_num1.Text) < 0 Then
            MsgBox("Please insert a number greater than zero")
            text_num2.Enabled = False

        ElseIf (text_num1.Text) > 100 Then

            MsgBox("Please insert a number less than one hundred")
            text_num2.Enabled = False

        Else

            Decimal.TryParse(text_num1.Text, num1)

            text_num2.Enabled = True    'Enable textbox 2 after all validations
            Bt_Clear2.Enabled = True    'Enable textbox 2 clear button after all validations

        End If

        'End - Section to verify if all the conditions were satisfied.


    End Sub

    'End of Input Session 1

    'Begin of Input Session 2

    Private Sub text_num2_TextChanged(sender As Object, e As EventArgs) Handles text_num2.TextChanged

        If text_num2.Text = Nothing Then
            MsgBox("Please insert a value.")
            text_num3.Enabled = False

        ElseIf IsNumeric(text_num2.Text) = False Then
            MsgBox("Please insert a number")
            text_num3.Enabled = False

        ElseIf (text_num2.Text) < 0 Then
            MsgBox("Please insert a number greater than zero")
            text_num3.Enabled = False

        ElseIf (text_num2.Text) > 100 Then

            MsgBox("Please insert a number less than one hundred")
            text_num3.Enabled = False

        Else

            Decimal.TryParse(text_num2.Text, num2)

            text_num3.Enabled = True    'Enable textbox 3 after all validations
            Bt_Clear3.Enabled = True    'Enable textbox 3 clear button after all validations

        End If


    End Sub


    'End of Input Session 2


    'Begin of Input Session 3

    Private Sub text_num3_TextChanged(sender As Object, e As EventArgs) Handles text_num3.TextChanged

        If text_num3.Text = Nothing Then
            MsgBox("Please insert a value.")

        ElseIf IsNumeric(text_num3.Text) = False Then
            MsgBox("Please insert a number")

        ElseIf (text_num3.Text) < 0 Then
            MsgBox("Please insert a number greater than zero")

        ElseIf (text_num3.Text) > 100 Then

            MsgBox("Please insert a number less than one hundred")

        Else

            Decimal.TryParse(text_num3.Text, num3)
            LabelCalcAverage.Enabled = True     'Enable the calculator button only after all fields were filled

        End If


    End Sub


    'End of Input Session 3


    'Caculation session
    Private Sub LabelCalcAverage_Click(sender As Object, e As EventArgs) Handles LabelCalcAverage.Click

        CalcAverage = (num1 + num2 + num3) / 3

        Tx_Average.Enabled = True


        'Function to show different colors on TextBox and Label

        If CalcAverage < 60 Then
            Tx_Average.ForeColor = Color.Yellow

            Tx_Average.BackColor = Color.Red

            Label_Average.BackColor = Color.Red

            Tx_Average.Text = CalcAverage.ToString("###.##")

        ElseIf CalcAverage >= 60 Then
            Tx_Average.ForeColor = Color.Yellow

            Tx_Average.BackColor = Color.Green

            Label_Average.BackColor = Color.Green

            Tx_Average.Text = CalcAverage.ToString("###.##")

        End If

        'End of function to show different colors on TextBox and Label


        'Disable all textbox and clear buttons after Calculate button was pressed

        text_num1.Enabled = False
        Bt_Clear1.Enabled = False

        text_num2.Enabled = False
        Bt_Clear2.Enabled = False

        text_num3.Enabled = False
        Bt_Clear3.Enabled = False

        'End of disable all textbox and clear buttons after Calculate button was pressed

    End Sub

    'End of caculation session


    'Function to clear individuals textbox

    Private Sub Bt_Clear1_Click(sender As Object, e As EventArgs) Handles Bt_Clear1.Click
        text_num1.Clear()
        num1 = Nothing
    End Sub

    Private Sub Bt_Clear2_Click(sender As Object, e As EventArgs) Handles Bt_Clear2.Click
        text_num2.Clear()
        num2 = Nothing
    End Sub

    Private Sub Bt_Clear3_Click(sender As Object, e As EventArgs) Handles Bt_Clear3.Click
        text_num3.Clear()
        num3 = Nothing
    End Sub

    'End of function to clear individuals textbox

    'Messagebox button
    Private Sub bt_about_Click(sender As Object, e As EventArgs) Handles bt_about.Click

        MsgBox("Program design by Edino - W0430397")

    End Sub

    'End of messagebox button

    'Function to clear all textbox
    Private Sub B_Reset_Click_1(sender As Object, e As EventArgs) Handles B_Reset.Click

        text_num1.Text = ""
        text_num2.Text = ""
        text_num3.Text = ""
        Tx_Average.Clear()
        CalcAverage = Nothing
        Tx_Average.BackColor = Color.White
        Label_Average.BackColor = Color.FromKnownColor(KnownColor.Control)
        Tx_Average.Enabled = False
        num1 = Nothing
        num2 = Nothing
        num3 = Nothing

        text_num1.Enabled = True
        Bt_Clear1.Enabled = True

        text_num2.Enabled = False
        Bt_Clear2.Enabled = False

        text_num3.Enabled = False
        Bt_Clear3.Enabled = False

        LabelCalcAverage.Enabled = False


        'Application.Restart() 'Another way to reset all TextBoxes but this one restart the application

    End Sub

    'End of function to clear all TextBoxes

End Class

Solution

  • Move you validating code to the Validating event of the TextBox.

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            TextBox2.Clear()
            'No MessageBox appears
    End Sub
    
    Private Sub TextBox2_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox2.Validating
            MessageBox.Show("Validating")
    End Sub