Possible Duplicate:
Create WPF TextBox that accepts only numbers
How i can check if inputed value in textbox contains text? I want to user input only numbers Thanks
Assuming you are using integers then:
int value = 0;
if(!Int32.TryParse(strInput, out value))
{
// Validation failed - show error or feedback to user
}
else
{
// Validation successful
}
For doubles, replace Int32.TryParse
with Double.TryParse
etc.
There is probably some fancy WPF way to do this as well (as indicated by V4Vendetta's comment).
You could you a regular expression to check for @"[^\d]"
if true there are non numbers
Alternatively @"^\d+$"
will match ints and @"\d+(\.\d+)?$"
will match decimals
Alternatively you could use a maskedtextbox control, either by embeding the winforms control using a host control or using something like Infragistics editor.
If you want only number check if you can parse it. If you want int
use int.Parse()
精彩评论