【发布时间】:2015-09-03 02:46:35
【问题描述】:
我正在开发一个相当基本的 VB 程序(没有双关语),我需要在其中将基本整数转换为罗马数字。我的转换部分与我的 Select Case 完美配合。我还需要添加验证输入,因此如果输入了无效数字,则文本框会显示为这样。 1 到 10 之间的任何数字都应该能够单击转换按钮。目前,我输入 1 到 10 之间的任何数字都会立即显示“该数字无效。”
这是我当前的代码,失败了:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub lblRomanNum_Click(sender As Object, e As EventArgs)
End Sub
Private Sub txtBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBox1.TextChanged
Dim intNum As Integer
If intNum < 1 Or intNum > 10 Then
txtBox1.Text = "That number is invalid."
'ElseIf intNum > 10 Then
'txtBox1.Text = "That number is invalid"
End If
End Sub
Private Sub txtBox2_TextChanged(sender As Object, e As EventArgs) Handles txtBox2.TextChanged
End Sub
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Select CInt(txtBox1.Text)
Case 1 ' numerical 1
txtBox2.Text = "I"
Case 2 ' numerical 2
txtBox2.Text = "II"
Case 3 ' numerical 3
txtBox2.Text = "III"
Case 4 ' numerical 4
txtBox2.Text = "IV"
Case 5 ' numerical 5
txtBox2.Text = "V"
Case 6 ' numerical 6
txtBox2.Text = "VI"
Case 7 ' numerical 7
txtBox2.Text = "VII"
Case 8 ' numerical 8
txtBox2.Text = "VIII"
Case 9 ' numerical 9
txtBox2.Text = "IX"
Case 10 ' numerical 10
txtBox2.Text = "X"
'Case Else
'If a user enters an invalid value, this message is displayed and no conversion is attempted, according to instructions.
'txtBox2.Text = "That value is invalid."
End Select
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub lblRomanNum_Click_1(sender As Object, e As EventArgs)
End Sub
End Class
任何小于 1 的 intNum 都应显示无效消息。
任何大于 10 的 intNum 都应显示无效消息。
如果我正在正确阅读我目前拥有的内容,这应该可以工作,并允许我输入 1 到 10 之间的数字,而不会出现无效消息。我在这里遗漏了什么吗?
【问题讨论】: