2016-02-26 81 views
-1

我不能得到以下vb.net代码太工作。我试图达到的目标是限制在3和6之间使用的数字。如果用户输入的值小于3,文本框将值更正为3,并且用户输入的值大于6,则文本框值已更改为6 ...Autocorrecting文本框输入(vb.net代码)

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 

回答

1

有关此问题的一些信息将会有所帮助。你是否遇到异常?我认为你需要在2和7附近引用一些引号。此外,文本比较(即TextBox27.Text <= "2")可能会被愚弄,具体取决于你应用程序中的其他代码。

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 
+0

是的尝试,但我仍然有同样的问题,如果我1型或2文本框作出响应,预期...但是如果我在任何类型lagrger比6 texbox剂量没有反应.. –

+0

这是因为' TextBox27.Text> =“7”'永远不会是真的。该框只接受小于7的字符。您正在进行的文本比较评估字母顺序。也许你应该将TextBox27.Text转换为一个整数,然后比较为7(不带引号。 – JerryM