Search code examples
vb.netformsvalidationmessageboxis-empty

Validation form vb.net


Stupid question!

I'm checking if the inputboxes are empty...but after the check, I want to navigate back to my form and give the user a second chance to change their input.

At this moment, the app will show a messagebox if it's empty, but he goes further in my code to the second check...is there a code where I can break the code an go back to the form?

My code:

    If naam = "" Then
        MessageBox.Show("Naam mag niet leeg zijn", "No entry",
        MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If


    prijsstr = TextBox2.Text
    If prijsstr = "" Then
        MsgBox("Prijs mag niet leeg zijn")
    ElseIf IsNumeric(prijsstr) = False Then
        MsgBox("Prijs moet numeriek zijn")
    Else
        prijs = Integer.Parse(prijsstr)
    End If

    If prijs < 0 Then
        MsgBox("Prijs mag niet onder 0 zijn")
    End If

Solution

  • Couldn't you just Return?

    If naam = "" Then
        MessageBox.Show("Naam mag niet leeg zijn", "No entry",
        MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return
    End If
    
    
    prijsstr = TextBox2.Text
    If prijsstr = "" Then
        MsgBox("Prijs mag niet leeg zijn")
        Return
    ElseIf IsNumeric(prijsstr) = False Then
        MsgBox("Prijs moet numeriek zijn")
        Return
    Else
        prijs = Integer.Parse(prijsstr)
    End If
    
    If prijs < 0 Then
        MsgBox("Prijs mag niet onder 0 zijn")
        Return
    End If