2013-08-24 126 views
-1

我想有一个文本框验证该条目是1和100之间的数字“之间”C#文本框验证

例子:

if (textBox.Text is equal to numbers between 1 and 100) 
{ 
    do this; 
} 
else 
{ 
    do this; 
} 

这是表单验证的跟踪条用于JPEG压缩,并且只能具有1到100之间的数值。我该怎么做?

enter image description here

+0

有什么技术你在用吗? WebForms,WinForms,WPF? – VsMaX

+1

我正在使用Windows窗体。 – TK421

+0

我会检查是否可以在其属性中设置跟踪栏的最小值和最大值。 – deepee1

回答

1
String text = TextBox.Text; 
try{ 
    long value = long.parse(text.trim()); 
    if(value > 0 && value < 101){ 
     //do something here 
    } 
    else{ 
     //Do something else 
    } 
} 
catch(Exception e){ 
    Messagebox.Show("Please check you input and try again"); 
} 
0

首先,你需要输入由文本从字符串转换为整数

string textBoxvalue = textBox.Text; 
int textBoxIntValue = int.TryParse(textBoxvalue) 

,那么你需要检查值状态,你需要

if(textBoxIntValue > 0 && textBoxIntValue <= 100) 
{ 
//do THIS 
}