Click here to Skip to main content
11,635,788 members (81,468 online)
Click here to Skip to main content

Getting control of your numbers

, 1 Apr 2011 CPOL 8.1K 6
Rate this:
Please Sign up or sign in to vote.
A simple mechanism to limit the input of data to a TextBox so that it only accepted the relevant numeric amount.

Today on Code Project, one of the regulars asked how to set up a textbox so that it only accepted a currency amount. He was concerned that there doesn’t seem to be a simple mechanism to limit the input of data so that it only accepted the relevant numeric amount. Well, this is a feature I recently added into Goldlight, so I thought I’d post it here, along with an explanation of how it works.

Basically, and this will come as no surprise to you, it’s an Attached Behavior that you associate to the TextBox. There are many numeric only behaviors out there, so this one goes a little bit further. First of all, if you want, you can limit it to integers by setting AllowDecimal to false. If you want to limit it to a set number of decimal places, set DecimalLimit to the number of decimal places. If you don’t want to allow the developer to use negative numbers, set AllowNegatives to false. It’s that simple, so the solution to the problem would be to add the behaviour to the TextBox like this:

<TextBox Text="{Binding Price}">
  <i:Interaction.Behaviors>
    <gl:NumericTextBoxBehavior AllowNegatives="False" />
  </i:Interaction.Behaviors>
</TextBox>

The full code to do this is shown below:

namespace Goldlight.Extensions.Behaviors
{
  using System.Windows.Controls;
  using System.Windows.Interactivity;
  using System.Windows.Input;
  using System.Text.RegularExpressions;
  using System.Windows;
  using System.Globalization;

  /// <span class="code-SummaryComment"><summary>
</span>

The clever thing is that this behavior doesn’t allow the user to paste an incorrect value in either – the paste operation is subject to the same rules as directly entering the value in the first place.

Anyway, I hope this behavior is as much use to you as it is to me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pete O'Hanlon
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

You may also be interested in...

Comments and Discussions

 
BugDoes not work with UpdateSourceTrigger=PropertyChanged Pin
d_weyermann24-Jan-12 21:51
memberd_weyermann24-Jan-12 21:51 
GeneralMy vote of 5 Pin
gardnerp3-May-11 3:40
membergardnerp3-May-11 3:40 
GeneralRe: My vote of 5 Pin
Pete O'Hanlon10-May-11 10:16
mvpPete O'Hanlon10-May-11 10:16 
Sorry it's taken so long for me to reply. For some reason I'm not getting notified of messages.

Anyway, if you need a max/min value, feel free to add the range as a property - but, consider what happens if you have a range of 0 to 500 and the user types 60 then presses 0. As you'd expect, it shouldn't let them enter the value, but this could be a surprise to the user. It all depends on your users, and how they react to the range checking.

Forgive your enemies - it messes with their heads

My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


QuestionPRE tags language attribute missing? Pin
Sandeep Mewara1-Apr-11 0:49
mvpSandeep Mewara1-Apr-11 0:49 
AnswerRe: PRE tags language attribute missing? Pin
Pete O'Hanlon1-Apr-11 1:30
mvpPete O'Hanlon1-Apr-11 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 1 Apr 2011
Article Copyright 2011 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid