VB.net 为整数验证文本框输入时崩溃
VB.net validating textbox input for integers crash
我有一个表单,用户需要在其中输入整数以在按下按钮
时保存到 xml
问题在于,当用户未输入有效输入时,它会显示错误,但仍会尝试为我的函数声明变量,然后崩溃 - 我该如何停止?
这是我的代码:
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
您可以尝试使用 TryParse
来确保整数值没有错误:
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
在以下位置找到更多信息:Int32.TryParse Method
将变量声明移到“If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then”中,这样如果 txtLevel 和 txtHealth 都是整数,它只会尝试为它们赋值.
我建议您像这样更改代码:
'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
您正在使用相当多的旧功能,并且您还关闭了 Option Strict。
- IsNumeric 应替换为 Integer.TryParse:https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse
- MsgBox 应替换为 MessageBox:https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox
- 有关 option strict 的更多信息,我建议您查看以下任何资源:
话虽如此,您的代码可能如下所示:
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
我有一个表单,用户需要在其中输入整数以在按下按钮
时保存到 xml
问题在于,当用户未输入有效输入时,它会显示错误,但仍会尝试为我的函数声明变量,然后崩溃 - 我该如何停止?
这是我的代码:
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
您可以尝试使用 TryParse
来确保整数值没有错误:
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
在以下位置找到更多信息:Int32.TryParse Method
将变量声明移到“If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then”中,这样如果 txtLevel 和 txtHealth 都是整数,它只会尝试为它们赋值.
我建议您像这样更改代码:
'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
您正在使用相当多的旧功能,并且您还关闭了 Option Strict。
- IsNumeric 应替换为 Integer.TryParse:https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse
- MsgBox 应替换为 MessageBox:https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox
- 有关 option strict 的更多信息,我建议您查看以下任何资源:
话虽如此,您的代码可能如下所示:
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