none
How to prevent a user from entering certain characters in a textbox

    Question

  • Hello,

    I have a user control with a textbox for user input, and would like to limit the user to only typing certain characters into the textbox i.e. the user types an "a" and it doesn't show up, because it's not valid.  Depending on the custom dependency property settings of the user control, I would like the user to only be allowed to enter numeric characters, and possibly '.' and '-' to support negatives/decimals when allowed.

    My thought it to somehow use TextChangedEventArgs to test for certain values, then revert back to the last state if not valid.  Is this possible???

    Before, I explicitly updated the binding source with a validation rule on TextChanged, but now I would also like to format numbers using StringFormat set to F3.  Using both of these gives this result:

    type: 1
    get: 1.000

    type: 1.345
    get: 1.000 (because it fills in the .000 from the UpdateSource on the TextChanged from typing '1' before it gets to '.345')

    This is annoying behavior.  Any suggestions on how to use StringFormat and Validation Rules together, or to use TextChangedEventArgs to find out if the last entered character was valid, and then remove it if not?

    Many thanks!
    Monday, April 13, 2009 9:00 PM

Answers

  • The keyword here is "dependency property". 

    Try the WPF Forums:

    http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=119


    David Morton - http://blog.davemorton.net/
    • Marked as answer by MereS Tuesday, April 14, 2009 1:18 PM
    Monday, April 13, 2009 9:04 PM
  • //Example

    public class CustomTextBox : System.Windows.Forms.TextBox
    {
          protected override void OnKeyPress(KeyPressEventArgs e)
          {
                base.OnKeyPress(e);
                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                    e.Handled = true;
          }
    }
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by MereS Tuesday, April 14, 2009 1:18 PM
    Monday, April 13, 2009 9:06 PM
  • Yes, TextChanged is the right way if you want to solve the problem quickly. But you know what will be the behaviour of the validation. TextChanged is happening when you move the focus out from the text box, so you can't prevent user to type illegal characters.
    Of course you can solve this perfectly if you use some other events like KeyPress or simular ones. But you should handle not just key press but also other actions in the textbox like Paste operation. It is not easy but is posible. You can check my posts here if you like the harder way which can be used in windows applications.
    Isn't it posible that some control like MaskedEditBox or numericupdown to be used in your scenario?
    I don't know what is the technology that you are using, but if that is asp.net, you can use some new ways like AjaxControlToolkit's FilteredTextBoxExtender.
    MCDBA, MCSD, MCITP DD&DA http://sharpsource.blogspot.com/
    • Marked as answer by MereS Tuesday, April 14, 2009 1:19 PM
    Monday, April 13, 2009 10:19 PM

All replies

  • The keyword here is "dependency property". 

    Try the WPF Forums:

    http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=119


    David Morton - http://blog.davemorton.net/
    • Marked as answer by MereS Tuesday, April 14, 2009 1:18 PM
    Monday, April 13, 2009 9:04 PM
  • //Example

    public class CustomTextBox : System.Windows.Forms.TextBox
    {
          protected override void OnKeyPress(KeyPressEventArgs e)
          {
                base.OnKeyPress(e);
                if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                    e.Handled = true;
          }
    }
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by MereS Tuesday, April 14, 2009 1:18 PM
    Monday, April 13, 2009 9:06 PM
  • Yes, TextChanged is the right way if you want to solve the problem quickly. But you know what will be the behaviour of the validation. TextChanged is happening when you move the focus out from the text box, so you can't prevent user to type illegal characters.
    Of course you can solve this perfectly if you use some other events like KeyPress or simular ones. But you should handle not just key press but also other actions in the textbox like Paste operation. It is not easy but is posible. You can check my posts here if you like the harder way which can be used in windows applications.
    Isn't it posible that some control like MaskedEditBox or numericupdown to be used in your scenario?
    I don't know what is the technology that you are using, but if that is asp.net, you can use some new ways like AjaxControlToolkit's FilteredTextBoxExtender.
    MCDBA, MCSD, MCITP DD&DA http://sharpsource.blogspot.com/
    • Marked as answer by MereS Tuesday, April 14, 2009 1:19 PM
    Monday, April 13, 2009 10:19 PM
  • Thank you!  All of your comments were very helpful.
    Tuesday, April 14, 2009 1:19 PM