Vb.net 整数在有效均值内时输入无效?
我正在开发一个VB程序,相当基本(没有双关语),在这个程序中,我需要将基本整数转换为罗马数字。我有转换部分与我的选择完美的情况。我还需要添加验证输入,这样如果输入了无效的数字,文本框就会显示出来。任何介于1和10之间的数字都可以单击“转换”按钮。目前,我输入的任何介于1和10之间的数字都会立即显示“该数字无效” 这是我当前的代码,它失败了:Vb.net 整数在有效均值内时输入无效?,vb.net,vba,Vb.net,Vba,我正在开发一个VB程序,相当基本(没有双关语),在这个程序中,我需要将基本整数转换为罗马数字。我有转换部分与我的选择完美的情况。我还需要添加验证输入,这样如果输入了无效的数字,文本框就会显示出来。任何介于1和10之间的数字都可以单击“转换”按钮。目前,我输入的任何介于1和10之间的数字都会立即显示“该数字无效” 这是我当前的代码,它失败了: Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) H
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
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
端接头
私有子lblRomanNum\u单击(发送方作为对象,e作为事件参数)
端接头
私有子txtBox1_TextChanged(发送方作为对象,e作为事件args)处理txtBox1.TextChanged
Dim intNum作为整数
如果intNum<1或intNum>10,则
txtBox1.Text=“该数字无效。”
'ElseIf intNum>10则
'txtBox1.Text=“该数字无效”
如果结束
端接头
私有子txtBox2_TextChanged(发送方作为对象,e作为事件args)处理txtBox2.TextChanged
端接头
私有子btnConvert_Click(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理btnConvert。单击
选择CInt(txtBox1.Text)
案例1'数字1
txtBox2.Text=“I”
案例2’数字2
txtBox2.Text=“II”
案例3'数字3
txtBox2.Text=“III”
案例4’数字4
txtBox2.Text=“IV”
案例5'数字5
txtBox2.Text=“V”
案例6'数字6
txtBox2.Text=“VI”
案例7'数字7
txtBox2.Text=“VII”
案例8'数字8
txtBox2.Text=“VIII”
案例9'数字9
txtBox2.Text=“IX”
案例10'数字10
txtBox2.Text=“X”
”“还有别的吗
'根据说明,如果用户输入无效值,将显示此消息,并且不会尝试转换。
'txtBox2.Text=“该值无效。”
结束选择
端接头
私有子btnExit\u单击(发送者作为对象,e作为事件参数)处理btnExit。单击
我
端接头
私有子lblRomanNum\u Click\u 1(发送方作为对象,e作为事件参数)
端接头
末级
任何小于1的intNum都应显示无效消息
任何大于10的整数都应显示无效消息
如果我正确地阅读了当前的内容,这应该可以工作,并允许我输入一个介于1和10之间的数字,而不会出现无效消息。我在这里遗漏了什么吗?试试括号和“或”
1. Dim intNum As Integer
2. If (intNum < 1) or (intNum > 10) Then
3. txtBox1.Text = "That number is invalid."
4. End If
1。Dim intNum作为整数
2.如果(intNum<1)或(intNum>10),则
3.txtBox1.Text=“该数字无效。”
4.如果结束
您的代码在Visual Studio 2013中与.NET Framework 4.5.1完美结合。尝试删除整个块并重新键入代码。
或者你也可以试试这段代码
If intNum < 1 OrElse intNum > 10 Then
TextBox1.Text = "That number is invalid."
End If
如果intNum<1或lse intNum>10,则
TextBox1.Text=“该号码无效。”
如果结束
需要添加
Integer.TryParse(txtBox1.Text, i)
…直接在我的变量声明下 无法解释的是,您的建议仍然导致一条无效消息。然后您需要在另一个控件中显示您获得的值;也许这不是你认为应该的。无法解释的是,你的建议仍然会产生一个无效的信息。你已经改变了问题
Dim intNum As Integer
是声明,intNum
始终为0。因此,代码按预期工作。