范围窗口形式文本框C#.net

时间:2011-05-10 04:40:09

标签: c# windows textbox

我想允许用户只输入介于0到360之间的数字。如何在C#.Net Windows应用程序中使用文本框控件来完成?

2 个答案:

答案 0 :(得分: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);

   }
}

答案 1 :(得分:0)

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

您也可以尝试在用户输入时限制输入,但这可能会有点问题。例如,如果击键使结果超出范围,您会怎么做?当用户进行合法编辑时,如果该号码暂时超出范围会怎样?