开发者

How to check inputed contains text [duplicate]

开发者 https://www.devze.com 2023-03-20 20:36 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_Go百科 Possible Duplicate: Create WPF TextBox that accepts only numbers
This question already has answers here: Closed 11 years ago. 开发者_Go百科

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号