Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / WPF

WPF Maskable TextBox for Numeric Values

Rate me:
Please Sign up or sign in to vote.
4.76/5 (37 votes)
19 Mar 2009CPOL3 min read 206.9K   9.4K   70   29
Extension of the WPF TextBox which accepts only numeric (integer and floating point) values.

Introduction

This article describes how to enhance the WPF TextBox and make it accept just numeric (integer and floating point) values. The second goal is make the TextBox smart enough to make it easier to input numerics. This is an easy means to provide the TextBox with some kind of intelligence, not just rejecting non-numeric symbols. The provided extension also allows setting minimum and/or maximum values.

If you search in the net, you will probably find some solutions for this problem where developers create their own versions of the TextBox either by inheriting from it or creating a Custom/User Controls that include the standard WPF TextBox. Most other solutions have one major drawback - you would need to replace your TextBox definitions with your new MaskTextBox. Sometimes, it is not painful, sometimes, it is. The reason I chose another solution is that in my case, such kind of changes would be painful.

The approach I’m proposing here is the usage of WPF Attached Properties, which basically are similar to Dependency Properties. The major difference among these to is that Dependency Properties are defined inside the control, but Attached Properties are defined outside. For instance, TextBox.Text is a Dependency Property, but Grid.Column is an Attached Property.

Background (Extending the Functionality of the TextBox)

First of all, I will define an enumeration which tells us whether the TextBox accepts integer, decimal, or any kind of values:

C#
public enum MaskType  
{  
   Any,  
   Integer,  
   Decimal  
}

The next thing would be the definition of the Attached Property.

C#
public class TextBoxMaskBehavior
{
   public static MaskType GetMask(DependencyObject obj)
   {
      return (MaskType)obj.GetValue(MaskProperty);
   }

   public static void SetMask(DependencyObject obj, MaskType value)
   {
      obj.SetValue(MaskProperty, value);
   }

   public static readonly DependencyProperty MaskProperty =
      DependencyProperty.RegisterAttached(
      "Mask",
      typeof(MaskType),
      typeof(TextBoxMaskBehavior),
      new FrameworkPropertyMetadata(MaskChangedCallback)
      );

   private static void MaskChangedCallback(DependencyObject d, 
                       DependencyPropertyChangedEventArgs e)
   {
      // ...
   }
}

Now, we specify the mask for the WPF TextBox like this:

XML
<TextBox local:TextBoxMaskBehavior.Mask="Integer" />

or

XML
<TextBox local:TextBoxMaskBehavior.Mask="Decimal" />

and our assumption would be that the particular TextBox would accept integer and decimal values, respectively. Whenever TestBoxMaskBehavior.Mask is set, MaskChangedCallback is being called. We will add the corresponding handling there.

Background (Subscribing to the TextBox Changes)

The WPF TextBox comes with an event: PreviewTextInput, which allows to listen for text input. It also allows to cancel a particular text input by setting the Handled property of the TextCompositionEventArgs to true. Shown below is an illustrative example:

XML
<TextBox PreviewTextInput="TextBox_PreviewTextInput" />

The C# code:

C#
private static void TextBox_PreviewTextInput(object sender, 
        System.Windows.Input.TextCompositionEventArgs e)
{
   try
   {
      Convert.ToInt32(e.Text);
   }
   catch
   {
      e.Handled = true;
   }
}

Please note that this code is just an example of the PreviewTextInput event usage. My solution has much more useful features rather than checking whether the input text is a number or not :).

C#
DataObject.AddPastingHandler(myTextBox, TextBoxPastingEventHandler);
//...
private void TextBoxPastingEventHandler(object sender, DataObjectPastingEventArgs e)
{
   string clipboard = e.DataObject.GetData(typeof(string)) as string;
   try
   {
      Convert.ToInt32(clipboard);
   }
   catch
   {
      e.CancelCommand();
      e.Handled = true;
   }
}

I think that was self-explanatory :).

Maskable TextBox

We have covered the techniques that I used in the solution. The full source code and the demo binary are available from the links above. Here is a list of the features available for my Maskable TextBox:

  • Rejects symbols other than digits, negative signs, and the decimal separator.
  • Clamps to minimum/maximum values, if any specified.
  • When typing negative sign, regardless from the caret position, adds it in the beginning if not exist, or removes the existing one.
  • When typing the decimal separator, removes the existing one (if exists), and places the new one in the correct place.
  • And, more useful stuff. Just try to use it. I’m sure you will not regret.

Origin

The origin of this article can be found at WPF Maskable TextBox for Numeric Values.

License

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


Written By
Software Developer (Senior) Independent Contractor
United States United States

Comments and Discussions

 
GeneralGenius Pin
DotDue20-Oct-09 9:58
DotDue20-Oct-09 9:58 
GeneralRe: Genius Pin
Yulian Ustiyanovych23-Nov-09 22:02
Yulian Ustiyanovych23-Nov-09 22:02 
GeneralRe: Genius Pin
Craig Beuker5-Apr-10 7:21
Craig Beuker5-Apr-10 7:21 
GeneralRe: Genius Pin
rjvenugopal27-Jan-13 20:08
rjvenugopal27-Jan-13 20:08 

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

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