Reputation: 3
I have a form in where users need to input integers to save to an xml when they press a button
the issue lies that when the user does not put in valid input, it displays the error but still attempts to declare the variables for my function and then crashes - how do i stop this?
here's my code:
If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then
MsgBox("choose a location to save in")
ElseIf String.IsNullOrWhiteSpace(txtHealth.Text) Or String.IsNullOrWhiteSpace(txtLevel.Text) Then
MsgBox("you have not filled in all fields")
Me.Close()
Else MsgBox("please input number form")
End If
Dim strName As String = txtCharacter.Text
Dim intLevel As Integer = txtLevel.Text
Dim intHealth As Integer = txtHealth.Text
Dim strStat As String = cmbStat.Text
Upvotes: 0
Views: 312
Reputation: 6131
You are using quite a few legacy features and you also have Option Strict turned off.
With that being said, your code could look like this:
Dim strName As String = txtCharacter.Text
Dim strStat As String = cmbStat.Text
Dim intLevel, intHealth As Integer
' if either condition is true, close the form prematurely
If (String.IsNullOrWhitespace(txtLevel.Text) OrElse String.IsNullOrWhitespace(txtHealth.Text)) Then
MessageBox.Show("You have not filled in all fields.", "Invalid Form")
Me.Close()
ElseIf (Not Integer.TryParse(txtLevel.Text, intLevel) OrElse Not Integer.TryParse(txtHealth.Text, intHealth)) Then
MessageBox.Show("Please input number form.", "Invalid Form")
Me.Close()
End If
' continue on with the code
Upvotes: 4
Reputation: 40
Move your variable declaration inside your "If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then" so its only going to try to assign them the value if both txtLevel and txtHealth are integers.
I would suggest you to change the code like so:
'Checks if the textboxes are not empty
If Not String.IsNullOrWhiteSpace(txtHealth.Text) Or String.IsNullOrWhiteSpace(txtLevel.Text) Then
'They are not empty, so the program checks if they are integers
If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then
'The input is integer
Dim strName As String = txtCharacter.Text
Dim intLevel As Integer = txtLevel.Text
Dim intHealth As Integer = txtHealth.Text
Dim strStat As String = cmbStat.Text
MsgBox("choose a location to save in")
Else
'The input is not integer
MsgBox("Value is not an integer")
End If
Else
'A textbox is empty
MsgBox("you have not filled in all fields")
Me.Close()
End If
Upvotes: 1
Reputation: 678
you can try with TryParse
to be sure of integer value without error :
Dim intLevel As Integer
Dim intHealth As Integer
If (Integer.TryParse(txtLevel.Text, intLevel)) And (Integer.TryParse(txtHealth.Text, intHealth)) Then
MsgBox("choose a location to save in")
ElseIf String.IsNullOrWhiteSpace(txtHealth.Text) Or String.IsNullOrWhiteSpace(txtLevel.Text) Then
MsgBox("you have not filled in all fields")
Me.Close()
Else MsgBox("please input number form")
End If
Dim strName As String = txtCharacter.Text
Dim strStat As String = cmbStat.Text
find more about it on : Int32.TryParse Method
Upvotes: 0