DaveDev
DaveDev

Reputation: 42185

How can I restrict the values a user enters into TextBox?

Users of my application can enter values into TextBoxes. In some cases the values need to be restricted to a certain range, e.g. 0 to 1, or 1 to 50. In other cases they can only enter upto 13 or 20 characters depending.

Can I use Binding.StringFormat to enforce these ranges? If so,

If not, what's the best way to implement this?

Upvotes: 3

Views: 7779

Answers (4)

Kiran
Kiran

Reputation: 47

The text box length property should solve

Textbox.maxlength= 10;

Upvotes: -1

Matten
Matten

Reputation: 17631

What you want is input validation. For starters this tutorial on MSDN will help you, more information on IDataErrorInfo (preferred way) can be obtained here.

This question was already a topic on SO: WPF Data Binding and Validation Rules Best Practices

Upvotes: 3

Habib
Habib

Reputation: 223257

You need to use Input mask, Check out this article WPF Maskable TextBox for Numeric Values

Based on this article in Events TextBox_PreviewTextInput and TextBoxPastingEventHandler you could check for a range.

Upvotes: 1

Joshua Drake
Joshua Drake

Reputation: 2746

The Vaildation in WPF Code Project should give you a good start.

Also the related question WPF Validators like ASP.NET should assist.

Upvotes: 1

Related Questions