Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please show me a way to check input textbox data is 0-9 only.
I made like this and it is OK.
<textbox keydown="TextBox_KeyDown" name="txtbox" width="250" height="20" grid.row="4" grid.column="2" horizontalalignment="Left" margin="4">

C#
private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            bool isDigit = e.Key >= Key.D0 && e.Key <= Key.D9;

            if (isDigit) { }
            else
                e.Handled = true;

}

But I want to use "DecimalDigit ::= '0'-'9'" in xaml file and so it is a little difficulty for me.
Thanks in advance...
Posted

1 solution

Hi chitswemyint, there is many ways to do that.
I have one in my mind right now:
1. Extend the TextBox class.
2. Add a property called "Validation".
3. Add a DependencyProperty that works with Validation property.
4. Create a method that use Regex to validate your textbox.
5. in your XAML you can add the Validation property as you wish.

Happy coding.

Regards.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900