2011-05-10 67 views

回答

3

从你的问题历史来看,我假设你在谈论c#和.Net框架。如果你想这样做,使TextBox控件验证属性(其设置为true)

然后定义验证函数

private void textBox1_Validating(object sender, 
       System.ComponentModel.CancelEventArgs e) 
{ 
    int input = 0; 
    bool isNum = Int32.TryParse(textBox1.Text,out input); 

    if(!isNum || input < 0 || input > 360) 
    { 
     // Cancel the event and select the text to be corrected by the user. 
     e.Cancel = true; 
     textBox1.Select(0, textBox1.Text.Length); 

    } 
} 
+0

错误...我想你的意思是在那里的一个'int.Parse()'? – 2011-05-10 04:51:09

+0

@Jonathan哦对。谢谢 ! – Pepe 2011-05-10 04:52:09

0

最简单的方法是简单地检查值当用户点击提交按钮。如果超出范围,您可以弹出消息。

您也可以尝试限制用户输入的输入,但这可能有点问题。例如,如果按键使结果超出范围,你会怎么做?如果用户进行合法编辑时该数字暂时超出范围,会发生什么情况?

+0

验证事件负责处理所有这些情况,因为一旦编辑的控件失去焦点,它就会被触发。验证事件作为其名称意味着验证输入时使用的正确事件。 – Pepe 2011-05-10 05:39:15