CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Oct 2006
    Posts
    170

    Talking Numerical in textbox

    Hi All ,

    i have a textbox and i need to limit the user input to this textbox to numerical values ,

    so he cannot type strings just 1 2 3 and so on ,

    i mena like the windwos calculator doens't accept strings or such charchters ,

    how to do that ?

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Numerical in textbox

    Take a look here: How to: Create a Numeric Text Box .

    A very simple solution is to handle the textbox keypress event:
    Code:
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = !(Char.IsDigit(e.KeyChar) );
            }
    Laitinen
    Last edited by laitinen; July 13th, 2007 at 04:14 PM.

  3. #3
    Join Date
    Oct 2006
    Posts
    170

    Re: Numerical in textbox

    o.k i'll try it now

  4. #4
    Join Date
    Oct 2006
    Posts
    170

    Re: Numerical in textbox

    i tried it , but it doens't work ,

    even there are no errors , that means my appliaction accepts it but the textbox still accept both numeric and string numbers ,

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

  6. #6
    Join Date
    Nov 1999
    Location
    Denmark
    Posts
    260

    Re: Numerical in textbox

    i seem to recall .net2 offering a masked text bos as a component that would do it i would suppose

  7. #7
    Join Date
    Jan 2007
    Posts
    491

    Re: Numerical in textbox

    I think it has to be like this:
    Code:
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = Char.IsDigit(e.KeyChar);
            }
    (Without the !)

    Anyway here's another one that I built for my last application, it worked for me...
    Code:
    using System.Text.RegularExpressions;
    ...
            private void t_KeyDown(object sender, KeyEventArgs e)
            {
                if (!Regex.IsMatch(e.KeyCode.ToString(), @"\d"))
                {
                    e.SuppressKeyPress = true;
                }
            }
    [Edit: oops, forgot to remove the backspace and spacebar...
    I edited the code]
    Last edited by Talikag; July 13th, 2007 at 05:02 PM.

  8. #8
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Numerical in textbox

    Quote Originally Posted by Talikag
    I think it has to be like this:
    Code:
     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = Char.IsDigit(e.KeyChar);
            }
    (Without the !)
    No, you want to set the key press to handled only if the character is not a decimal digit. If it is a decimal digit you want the key press to be handled and added to the textbox.

    Laitinen

  9. #9
    Join Date
    Jan 2007
    Posts
    491

    Re: Numerical in textbox

    Quote Originally Posted by laitinen
    No, you want to set the key press to handled only if the character is not a decimal digit. If it is a decimal digit you want the key press to be handled and added to the textbox.

    Laitinen
    isn't e.Handled = true means the KeyChar will NOT appear in the textbox? That's what I thought so far...

  10. #10
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Numerical in textbox

    Quote Originally Posted by Talikag
    isn't e.Handled = true means the KeyChar will NOT appear in the textbox? That's what I thought so far...
    Yes that is right. And you do not want it to appear if it is not a digit, right?

  11. #11
    Join Date
    Jan 2007
    Posts
    491

    Re: Numerical in textbox

    Quote Originally Posted by laitinen
    Yes that is right. And you do not want it to appear if it is not a digit, right?
    Yea, I got it. It's confusing :S

    Emm... Anyway... Both ways should work. The problem is probably the fact that you didn't add an event handler in the *.disigner.cs, like this:
    Code:
    this.textBox1.Click += new System.EventHandler(this.textBox1_Click); //Not in this case, it is just an example.

  12. #12
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Numerical in textbox

    You can use NumUpDown Control.

    Using KeyPress is also right. just add the handle for the event
    Code:
    this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);

  13. #13
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Numerical in textbox

    even though its ugly I'd recomend using the up/down control too...

    if you're going to make a numeric only textbox you have to also watch for paste...

    its easier in .net 2.0 because they added the SuppressKeyPress but I wrote a numeric only textbox control a long time ago that you could take a look at. I overrode the PreProcessMessage, but you can do pretty much the same thing in the OnKeyDown event and set supress key to true if its not numeric. handling the paste operation will be the same as what I wrote though.

  14. #14
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Numerical in textbox

    Quote Originally Posted by MadHatter
    even though its ugly I'd recomend using the up/down control too...
    Yes it is very ugly indeed! And not very useful at all in many situations. How would a numeric up/down control work if the number has a large range and/or needs high accuracy??

    Imagine you need to fill in how much tax you payed last year. You will need the accurate sum and you will need a range from 0 to sevaral millions, with a step of 1 dollar. Up/down control is useless here.

    So as I have said many times, a numeric up/down control might be a good solution, but it is highly dependent on the nature of the number you want to store.

    Cheers,

    Laitinen

  15. #15
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Numerical in textbox

    I like the idea of Rudegar ( the Masked Textbox ) too, it can be easily customised for your purpose. I'd try that first before trying a Numeric Updown.

Page 1 of 2 12 LastLast

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