我正在使用visual studio 2013中的vb.net开发一个简单的标记评分系统。这是我的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text >= 0 And TextBox1.Text <= 50 Then
TextBox4.Text = 1
End If
If TextBox1.Text >= 51 And TextBox1.Text <= 100 Then
TextBox4.Text = 0
End If
If TextBox2.Text >= 0 And TextBox2.Text <= 50 Then
TextBox5.Text = 1
End If
If TextBox2.Text >= 51 And TextBox2.Text <= 100 Then
TextBox5.Text = 0
End If
If TextBox3.Text >= 0 And TextBox3.Text <= 50 Then
TextBox6.Text = 1
End If
If TextBox3.Text >= 51 And TextBox3.Text <= 100 Then
TextBox6.Text = 0
End If
TextBox7.Text = Int(TextBox1.Text) + Int(TextBox2.Text) + Int(TextBox3.Text)
End Sub
在此,在计算是否有任何空文本框时,代码不起作用并给出错误。
我使用此代码绕过空文本框。
If Textbox1.Text = ""
Textbox1.Text = 0 'or 30
End If
但在计算等级时,此空数据包括软件0-50。因此它给出了错误的结果。将数字应用于空文本框是没用的。
请告诉我如何在计算时跳过/忽略或绕过空文本框?
答案 0 :(得分:0)
如果你和你在那里多一点 - 我已经为你的代码添加了一个函数,因为看起来所有三个框都需要相同的标准?如果没有,你必须单独指定每一个。
希望这是有道理的。它使用一个确定来查看文本框是否实际有效,还检查是否为数字,然后检查该数字是否有效。
如果有好处,请告诉我们!
Hth Chicken
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t1 As String = TextBox1.Text
Dim t2 As String = TextBox2.Text
Dim t3 As String = TextBox3.Text
Dim a1 As Integer = Get_result(t1)
Dim a2 As Integer = Get_result(t2)
Dim a3 As Integer = Get_result(t3)
TextBox4.Text = a1
TextBox5.Text = a2
TextBox6.Text = a3
Dim Sumed As Integer = 0
'If -1 then we dont use this number...
If Not a1 = -1 Then Sumed += a1
If Not a2 = -1 Then Sumed += a2
If Not a3 = -1 Then Sumed += a3
TextBox7.Text = Sumed
End Sub
Function Get_result(t As String) As Integer
'Returns -1 if this isnt a valid answer, or blank etc...
If t.Length = 0 Then Return -1
If Not IsNumeric(t) Then Return -1
Select Case CInt(t)
Case Is < 0
Return -1
Case Is < 51
Return 1
Case Is < 101
Return 0
Case Else
Return -1
End Select
End Function