我在 vb.net 上开发一个小程序,问题是我有两个文本框名称 和 年龄 (这是数组(。 我正在尝试这样做,以便如果输入名称而年龄不是,它将出现"请输入年龄",而不向数组添加任何内容,数组已尝试但不断收到"附加信息: 从字符串"转换为类型"双精度"无效"。我把我的代码放在下面,看看你是否能看到任何问题。
PS我只是一个初学者。 谢谢。我所看到的图像
"存储在阵列中" 暗淡年龄为字符串 将姓氏暗为字符串
Surnames = txtNames.Text
Ages = txtAge.Text
If txtNames.Text = "" Then
MsgBox("Enter Name ")
txtNames.Focus()
SystemValueAge = SystemValueAge - 1
SystemValueName = SystemValueName - 1
txtNames.Focus()
ElseIf Ages < 1 Or Ages > 100 Then
MsgBox("Please Enter Valid Age")
SystemValueAge = SystemValueAge - 1
SystemValueName = SystemValueName - 1
txtAge.Focus()
ElseIf txtAge.Text = "" Then
MsgBox("Enter Age")
SystemValueAge = SystemValueAge - 1
SystemValueName = SystemValueName - 1
txtAge.Focus()
End If
执行比较之前,您需要将txtAge.Text
转换为整数。另外,请确保将ElseIf txtAge.Text = "" Then
放在Convert.toInt32(Ages) < 1 Or Convert.toInt32(Ages) > 100
之前,如下所示:
ElseIf txtAge.Text = "" Then
MsgBox("Enter Age")
SystemValueAge = SystemValueAge - 1
SystemValueName = SystemValueName - 1
txtAge.Focus()
ElseIf Convert.toInt32(Ages) < 1 Or Convert.toInt32(Ages) > 100 Then
MsgBox("Please Enter Valid Age")
SystemValueAge = SystemValueAge - 1
SystemValueName = SystemValueName - 1
txtAge.Focus()
End If
原因是您需要先插入ElseIf txtAge.Text = ""
是因为如果字符串为空,则控制将不会进入以下ElseIf
语句,因此您不会得到FormatException
异常。
作为旁注,您应该使用ElseIf String.IsNullOrEmpty(txtAge.Text)
而不是ElseIf txtAge.Text = ""
。