因此,我有一个程序可以跟踪每个棒球比赛后的得分,并在ListBox中将每个连续比赛的得分相加。
我唯一遇到麻烦的部分是用户验证,以确保输入为数字并且不能传递非数字值。
最简单的方法是什么?必须有一个列表框和一个输入框。
代码如下:
Public Class Form1
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim intRunningTotal As Integer = 0
ListBox1.Items.Clear()
For inning = 1 To 7 Step 1
Dim strScore As String = InputBox("Enter the score of inning " + inning.ToString(), "Score")
Dim intScore As Integer = strScore
intRunningTotal = intRunningTotal + intScore
ListBox1.Items.Add(inning.ToString() + vbTab + intScore.ToString() + vbTab + intRunningTotal.ToString())
Next
End Sub
End Class
答案 0 :(得分:1)
代替For循环和InputBox,将TextBox添加到您的Form中并将其用作输入机制。就像在Sebastian的示例中一样,每次按下Button时,使用Integer.TryParse()将TextBox解析为Integer(每次单击按钮仅一个数字)。一旦您在ListBox中有七个内容,就可以禁用Button和TextBox,以便不再进行任何输入(或者可以使Button单击以告诉用户他们已经具有七个输入):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static intRunningTotal As Integer = 0
Dim intScore As Integer
If Integer.TryParse(TextBox1.Text, intScore) Then
If intScore >= 0 Then
intRunningTotal = intRunningTotal + intScore
Dim entry As String = (ListBox1.Items.Count + 1).ToString() + vbTab + intScore.ToString() + vbTab + intRunningTotal.ToString()
ListBox1.Items.Add(entry)
If ListBox1.Items.Count = 7 Then
Button1.Enabled = False
TextBox1.Enabled = False
End If
Else
MessageBox.Show("Score must be greater than or equal to zero.")
TextBox1.Focus()
TextBox1.SelectAll()
End If
Else
MessageBox.Show("Invalid Score. Please try again.")
TextBox1.Focus()
TextBox1.SelectAll()
End If
End Sub
对不起,但我忘了在原始帖子中提及我 仅限使用InputBox(弹出对话框),不能使用 文本框(嵌入在用户界面中)。该按钮必须触发一个输入框才能 弹出。分数必须在列表框中。不允许其他任何事情
这是另一个版本,然后:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim valid As Boolean
Dim intRunningTotal As Integer = 0
For inning As Integer = 1 To 7
Do
valid = False
Dim strResponse As String = InputBox("Enter the score of inning " + inning.ToString() & ": ", "Score")
Dim intScore As Integer
If Integer.TryParse(strResponse, intScore) Then
If intScore >= 0 Then
valid = True
intRunningTotal = intRunningTotal + intScore
ListBox1.Items.Add(inning.ToString() + vbTab + intScore.ToString() + vbTab + intRunningTotal.ToString())
Else
MessageBox.Show("Score must be greater than or equal to zero.")
End If
Else
MessageBox.Show("Invalid Score. Please try again.")
End If
Loop While Not valid
Next
End Sub
该代码中的其他用户代码也发生了相同的问题 在以下情况下提供了您无法突破输入框的位置: 取消或单击x,它将显示为无效分数 爆发。
您没有将此列为要求。 输入为空并单击“确定”与单击“ X”或“取消”之间没有区别。您可以检查是否返回了空字符串,并按如下所示退出:< / p>
Dim strResponse As String = InputBox("Enter the score of inning " + inning.ToString() & ": ", "Score")
If strResponse = "" Then
Exit For ' ...or Exit Sub
End If
这是InputBox的众所周知的局限性,也是它很少使用的原因之一。
答案 1 :(得分:0)
Integer.TryParse(string,integerVariable)将检查字符串以查看是否可以将其转换为Integer。它将返回True或False,因此可以在If状态下使用。另外,它用字符串的整数表示填充integerVariable。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim intRunningTotal As Integer = 0
ListBox2.Items.Clear()
Dim inning As Integer = 1
'changed to a do loop so I could increment inning in the If statement
'It will only increment if the parse is succesful
Do
Dim strScore As String = InputBox("Enter the score of inning " + inning.ToString(), "Score")
Dim intScore As Integer
If Integer.TryParse(strScore, intScore) Then
'This is a shortcut way to write intRunningTotal = intRunningTotal + intScore
intRunningTotal += intScore
ListBox2.Items.Add(inning.ToString() + vbTab + intScore.ToString() + vbTab + intRunningTotal.ToString())
inning += 1
Else
MessageBox.Show("Please enter a number")
End If
Loop While inning < 8
End Sub