.net 如何创建数组并放入列表框
我有这个代码,它似乎工作得很好。唯一的问题是,如果我在文本框中,然后双击按钮或双击enter键,我会在这里得到一个异常 “如果GotxtBox.Text<10,则” 它说“无效案例异常未处理” “从字符串到类型double的转换无效”如何阻止这种情况发生.net 如何创建数组并放入列表框,.net,vb.net,.net,Vb.net,我有这个代码,它似乎工作得很好。唯一的问题是,如果我在文本框中,然后双击按钮或双击enter键,我会在这里得到一个异常 “如果GotxtBox.Text
Private Sub GoBtn_Click(sender As Object, e As EventArgs) Handles GoBtn.Click
If GotxtBox.Text < 10 Then
MessageBox.Show("Number can not be less than 10")
GotxtBox.Clear()
Return
End If
If GotxtBox.Text > 100 Then
MessageBox.Show("Number can not be greater than 100")
GotxtBox.Clear()
Return
End If
Dim number As Integer = Val(GotxtBox.Text) ' get number
' add the number to the end of the numberListBox
GoLstBox.Items.Add(number)
If GoLstBox.Items.Count = 20 Then
GoBtn.Enabled = False
MessageBox.Show("Exactly Twenty Numbers Must Be Entered")
End If
GotxtBox.Clear()
GotxtBox.Focus()
End Sub
Private Sub GoBtn\u Click(发送方作为对象,e作为事件参数)处理GoBtn。单击
如果GotxtBox.Text<10,则
MessageBox.Show(“数字不能小于10”)
GotxtBox.Clear()
回来
如果结束
如果GotxtBox.Text>100,则
MessageBox.Show(“数字不能大于100”)
GotxtBox.Clear()
回来
如果结束
整数形式的数字=Val(GotxtBox.Text)'获取数字
'将数字添加到数字列表框的末尾
GoLstBox.Items.Add(编号)
如果GoLstBox.Items.Count=20,则
GoBtn.Enabled=False
MessageBox.Show(“必须准确输入二十个数字”)
如果结束
GotxtBox.Clear()
GotxtBox.Focus()
端接头
文本框包含文本,“10”与值10不同。您需要在比较之前转换文本。查看Integer.Tryparse
和/或Convert.ToInt32
稍后可以使用Val
执行类似操作,但也应该将其更改为TryParse
。净Val与VB6 Val不同。Dim text as String=GotxtBox.text
Dim text As String = GotxtBox.Text
Dim value As Double
If Double.TryParse(text, value) Then
If value < 10 Then
MessageBox.Show("Number can not be less than 10")
GotxtBox.Clear()
Return
End If
If value > 100 Then
MessageBox.Show("Number can not be greater than 100")
GotxtBox.Clear()
Return
End If
Else
'error, cannot convert
GotxtBox.Clear()
Return
End If
双精度模糊值
如果是Double.TryParse(文本,值),则
如果值小于10,则
MessageBox.Show(“数字不能小于10”)
GotxtBox.Clear()
回来
如果结束
如果值>100,则
MessageBox.Show(“数字不能大于100”)
GotxtBox.Clear()
回来
如果结束
其他的
'错误,无法转换
GotxtBox.Clear()
回来
如果结束
您的标题似乎与问题不匹配。我应该更改此选项,还是将Dim number设置为Integer=Val(GotxtBox.Text)'get number'将数字添加到numberListBox GoLstBox.Items.add的末尾(数字如果你不打算在解析过程中处理任何错误,你可以用if代替。)这是否像try-catch exception sort of…。基本上,Double.TryParse所做的是尝试将字符串转换为双精度值。如果不能,它将返回FALSE。你可以在ELSE块内执行某些操作,也可以将其完全忽略这取决于你的情况。我试过代码,但现在当你点击两次甚至一次按钮时,它在列表框中放了一个0。你不应该这样做吗