CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Numbers Only Textboxes!!

    Hi there!
    I am having some problem with user input validation.....what I want to do is use Numbers only Text box in WPF......While user gives an input it must not allow the use of strings or any other character.....Anyone have any idea on this??
    I have found a piece of code, but I am not able to use it correctly. If someone have better method please help me or give some insight on using this....Thanks......

    Code:
    public class NumberTextBox:TextBox
        {
    
            protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
                base.OnPreviewTextInput(e);
            }
    
         }


    Code:
    bool AreAllValidNumericChars(string str)
            {
                bool ret = true;
                if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
                    return ret;
    
                int l = str.Length;
                for (int i = 0; i < l; i++)
                {
                    char ch = str[i];
                    ret &= Char.IsDigit(ch);
                }
    
                return ret;
            }

    Its not that I have not tried using this code....Its just that I didn't have any idea where to use it...

  2. #2
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Re: Numbers Only Textboxes!!

    Although I haven't tested the above code , It should work just fine....but I have found and successfully used following bit of code.....Hope this Helps someone along the way.....

    Code:
    private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        foreach(char c in e.Text)
        {
            if (!char.IsDigit(c))
            {
                e.Handled = true;
                break;
            }
        }
    }
    for this method to work you need to select the exact textbox and go to eventlist button in the Property window......search for PreviewTextInput and double click it....it will automatically generate the event for you...now enter the code inside that event braces......
    This has some loop holes like it can take space, we can paste inside here and tab key for shifting between the various window element is absent etc.....so any one have idea maintaining that please help, although my initial requirement is fullfilled by this bit of code.....Thanks.....
    Last edited by rocky_upadhaya; March 7th, 2010 at 07:11 PM.

  3. #3
    Join Date
    Dec 2009
    Location
    Kathmandu,Nepal
    Posts
    168

    Re: Numbers Only Textboxes!!

    This method is similar to above but it accepts tabs too......
    Code:
     private void Number_PreviewTextInput(object sender, TextCompositionEventArgs e)
            {
                char c = e.Text.ToCharArray().First();
                e.Handled = !(char.IsNumber(c) || char.IsControl(c));
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured