自动更正文本框输入(vb.net代码)

自动更正文本框输入(vb.net代码),vb.net,if-statement,textbox,Vb.net,If Statement,Textbox,我不能得到下面的vb.net代码太多的工作。我想达到的目的是将使用的数字限制在3和6之间。如果用户输入的值小于3,则文本框会将该值更正为3,如果用户输入的值大于6,则文本框的值将更改为6 Select Case e.KeyChar Case "3", "4", "5", "6", vbBack e.Handled = False Case Else e.Handled = True If TextBox27.Text <= 2

我不能得到下面的vb.net代码太多的工作。我想达到的目的是将使用的数字限制在3和6之间。如果用户输入的值小于3,则文本框会将该值更正为3,如果用户输入的值大于6,则文本框的值将更改为6

Select Case e.KeyChar
    Case "3", "4", "5", "6", vbBack
        e.Handled = False
    Case Else
        e.Handled = True
        If TextBox27.Text <= 2 Then
            MessageBox.Show("Minimum of 3 loads permissible", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TextBox27.Text = "3"
            TextBox27.Focus()
        ElseIf TextBox27.Text >= 7 Then
            'Shows error message...
            MessageBox.Show("Maximum of 6 loads permissible", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TextBox27.Text = "6"
            TextBox27.Focus()
        End If

End Select
选择案例e.KeyChar
个案“3”、“4”、“5”、“6”及
e、 已处理=错误
其他情况
e、 已处理=真
如果TextBox27.Text=7,则
'显示错误消息。。。
MessageBox.Show(“允许最多加载6次”,“通知”,MessageBoxButtons.OK,MessageBoxIcon.Information)
TextBox27.Text=“6”
TextBox27.Focus()
如果结束
结束选择

关于这个问题的一些信息会很有帮助。你有例外吗?我想你需要一些关于2和7的引号。此外,文本比较(即
TextBox27.text是的)也尝试了这一点,但如果我键入1或2,文本框仍会像预期的那样响应…但是如果我键入任何大于6的内容,文本框都不会响应..这是因为
TextBox27.text>=“7”
永远不会为True。该框只接受小于7的字符。您正在进行的文本比较将按字母顺序进行计算。也许您应该将TextBox27.text转换为整数,然后与7(不带引号)进行比较。
Select Case e.KeyChar
    Case "3", "4", "5", "6", vbBack
        e.Handled = False
    Case Else
        e.Handled = True
        If TextBox27.Text <= "2" Then
            MessageBox.Show("Minimum of 3 loads permissible", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TextBox27.Text = "3"
            TextBox27.Focus()
        ElseIf TextBox27.Text >= "7" Then
            'Shows error message...
            MessageBox.Show("Maximum of 6 loads permissible", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)
            TextBox27.Text = "6"
            TextBox27.Focus()
        End If

End Select