Dim Number_of_courses As Integer
Number_of_courses = TextBox.Text
Number_of_courses > 0 and Number_of_courses < 8
我希望将用户的输入从1限制到8
假设WinForms…
使用整数。尝试解析将文本框中的字符串转换为整数。然后你可以检查它是否在
范围内Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Number_of_courses As Integer
If Integer.TryParse(TextBox1.Text, Number_of_courses) Then
If Number_of_courses > 0 And Number_of_courses < 8 Then
' ... do something with "Number_of_courses" in here ...
MessageBox.Show("Number_of_courses = " & Number_of_courses)
Else
MessageBox.Show("Invalid Number of Courses. Please enter a valid integer greater than zero and less then eight.")
End If
Else
MessageBox.Show("Invalid Number of Courses. Please enter a valid integer.")
End If
End Sub