CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Numbers Only Textboxes!!

    Rocky, a couple of things:

    1) please remember to post WPF related items in the WPF forum.
    2) when someone gives you code, try the code before asking other questions. For example, rather than speculating that the code won't work with TABS, you can load up the code and determine that yourself.

    As far as moving the post..., I'm going to leave this post in this forum because the solution is a C# solution.

    If you still need a WPF solution, please post another question in the WPF forum.

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

    Re: Numbers Only Textboxes!!

    Hi! Arjay, with regards
    Thaks for the suggestion. IT was stupid of me not to remember that I was working on WPF...And not trying code given by foamy, was all my bad but i had seen the same code on 1 other site where many users complained that tab key was not working,so i posted my reply... but I Beg my pardon to foamy for complaining before trying out myself...
    THANKS

  3. #18
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Numbers Only Textboxes!!

    Quick comment on Foamy's solution, that would still let people copy & paste invalid characters into the text box, wouldn't it?
    My hobby projects:
    www.rclsoftware.org.uk

  4. #19
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Numbers Only Textboxes!!

    Quote Originally Posted by Zaccheus View Post
    Quick comment on Foamy's solution, that would still let people copy & paste invalid characters into the text box, wouldn't it?
    EDIT: I should test things properly before posting
    It seems if you use the right click->insert method, you can insert invalid text to it ... I will have to take a look at that. Also, CTRL + V doesn't work


    There we go:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Globalization;
    
    namespace Foamy
    {
        public class NumericTextBox : TextBox
        {
            bool allowSpace = false;
            bool changing = false;
    
            // Restricts the entry of characters to digits (including hex), the negative sign,
            // the decimal point, and editing keystrokes (backspace).
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                base.OnKeyPress(e);
    
                NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
                string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
                string groupSeparator = numberFormatInfo.NumberGroupSeparator;
                string negativeSign = numberFormatInfo.NegativeSign;
    
                string keyInput = e.KeyChar.ToString();
    
                if (Char.IsDigit(e.KeyChar))
                {
                    // Digits are OK
                }
                else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
                 keyInput.Equals(negativeSign))
                {
                    // Decimal separator is OK
                }
                else if (e.KeyChar == '\b')
                {
                    // Backspace key is OK
                }
                else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
                {
                    // Let the edit control handle control and alt key combinations
                }
                else if (this.allowSpace && e.KeyChar == ' ')
                {
    
                }
                else
                {
                    // Consume this invalid key
                    e.Handled = true;
                }
            }
    
            protected override void OnTextChanged(EventArgs e)
            {
                if (!changing)
                {
                    changing = true;
                    //Check the text and make sure no invalid characters were pasted.
                    for (int i = this.Text.Length - 1; i >= 0; i--)
                    {
                        char c = this.Text[i];
                        if (!Char.IsDigit(c) && c != '\b' && !(this.allowSpace && c == ' '))
                            this.Text = this.Text.Remove(i, 1);
                    }
                    changing = false;
                }
                base.OnTextChanged(e);
            } 
    
            public int IntValue
            {
                get
                {
                    return Int32.Parse(this.Text);
                }
            }
    
            public decimal DecimalValue
            {
                get
                {
                    return Decimal.Parse(this.Text);
                }
            }
    
            public bool AllowSpace
            {
                set
                {
                    this.allowSpace = value;
                }
    
                get
                {
                    return this.allowSpace;
                }
            }
        }
    
    }
    Update: added base.OnTextChanged(e); to enable eventhandling by user
    Last edited by foamy; March 5th, 2010 at 05:29 AM.
    It's not a bug, it's a feature!

  5. #20
    Join Date
    Mar 2010
    Posts
    10

    Re: Numbers Only Textboxes!!

    This code by foamy is a work of art!
    Following on from what someone else had suggested - how can I validate the text when [OK] is pressed in my form?
    Sorry to ninja the thread, but I hope others would be wondering the same.

    Thanks

  6. #21
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Numbers Only Textboxes!!

    What sort of validation?

    You can handle the OK button press event, and do conditional checks there. Perhaps a regex validator. Plenty of options.

  7. #22
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Numbers Only Textboxes!!

    I still think vaildation is easier, either in response to the [Ok] button or when the Textbox loses focus.

    However, it may be better to do it at key stroke time, but that can lead to much more complicated code. This case would surely be better written as a UserControl beased on the original TextBox control.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  8. #23
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Numbers Only Textboxes!!

    Quote Originally Posted by Ibzy View Post
    This code by foamy is a work of art!
    Following on from what someone else had suggested - how can I validate the text when [OK] is pressed in my form?
    Sorry to ninja the thread, but I hope others would be wondering the same.

    Thanks
    Woah.. I can't take full credit for this. Although I wrote some of it, it's based on someone else's code. Probably from CodeProject.com, but I can't remember

    Quote Originally Posted by rliq View Post
    I still think vaildation is easier, either in response to the [Ok] button or when the Textbox loses focus.

    However, it may be better to do it at key stroke time, but that can lead to much more complicated code. This case would surely be better written as a UserControl beased on the original TextBox control.
    Easier when starting from scratch maybe. But with this thing you can just drag it into the form and not have to worry.
    I've never used UserControls, do you have an example of how this might work as one? It'd be a nice addition to the thread ^^
    Last edited by foamy; March 11th, 2010 at 02:34 AM.
    It's not a bug, it's a feature!

Page 2 of 2 FirstFirst 12

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