Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Only allow for a double in textbox

Only allow for a double in textbox

Scheduled Pinned Locked Moved C#
tutorialhelp
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...

    private void Price_KeyPress(object sender, KeyPressEventArgs e)
    {

            e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.');
    
            
        }
    
    J D D O H 5 Replies Last reply
    0
    • T tonyonlinux

      I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...

      private void Price_KeyPress(object sender, KeyPressEventArgs e)
      {

              e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.');
      
              
          }
      
      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      Use a NumericUpDown instead. It accepts only numeric input and you can specify in the designer how many decimal places it should use - no custom coding necessary.

      :badger:

      L 1 Reply Last reply
      0
      • T tonyonlinux

        I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...

        private void Price_KeyPress(object sender, KeyPressEventArgs e)
        {

                e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.');
        
                
            }
        
        D Offline
        D Offline
        Dan Mos
        wrote on last edited by
        #3

        as Jimmanuel said you could use the numeric... But I have found some small bugs in the numeric up down such: as you introduce some number then delete it and if you get the .Value it's not null or zero or... So, I had this problem too. Sorry I can find the code right now nor the time to write it but this is how you could do it: use the KeyDown event and then put the logic:

        //check if it already contains a dot '.' character by using the text property
        //if it does then allow only number and refuse everything else using
        e.Handled=true;
        e.SuppressKeyPress=true;
        //also check the number of chars after the '.' sign

        //else allow dot and numbers.

        use

        e.KeyCode == Keys.OemPeriod

        to check for '.' hope it helps

        L 1 Reply Last reply
        0
        • J Jimmanuel

          Use a NumericUpDown instead. It accepts only numeric input and you can specify in the designer how many decimal places it should use - no custom coding necessary.

          :badger:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Not quite, you can enter more decimals, its only when you Leave the Control that it gets rounded to the specified number of decimals. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          1 Reply Last reply
          0
          • D Dan Mos

            as Jimmanuel said you could use the numeric... But I have found some small bugs in the numeric up down such: as you introduce some number then delete it and if you get the .Value it's not null or zero or... So, I had this problem too. Sorry I can find the code right now nor the time to write it but this is how you could do it: use the KeyDown event and then put the logic:

            //check if it already contains a dot '.' character by using the text property
            //if it does then allow only number and refuse everything else using
            e.Handled=true;
            e.SuppressKeyPress=true;
            //also check the number of chars after the '.' sign

            //else allow dot and numbers.

            use

            e.KeyCode == Keys.OemPeriod

            to check for '.' hope it helps

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            just checking the latest keystroke as if it comes at the right of what is already there isn't sufficient: 1. the caret might not be at the end 2. you may want to support: copy, cut, paste, backspace... all of these require more than just checking the new character. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            [The QA section does it automatically now, I hope we soon get it on regular forums as well]


            D 1 Reply Last reply
            0
            • L Luc Pattyn

              just checking the latest keystroke as if it comes at the right of what is already there isn't sufficient: 1. the caret might not be at the end 2. you may want to support: copy, cut, paste, backspace... all of these require more than just checking the new character. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              [The QA section does it automatically now, I hope we soon get it on regular forums as well]


              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #6

              true. but I checked for stuff like that(if it's the delete key, back key etc) As for the position of the caret/cursor I didn't really cared cause I transformed .8686 in 0.8686 It really works for me. What I said here/before are some "guide lines". Again sorry I can't find the code. Just the *.dll. Thanks for the correction. I mean it. It should help TonyOnLinux too if he decides to implement he's own logic/control.

              1 Reply Last reply
              0
              • T tonyonlinux

                I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...

                private void Price_KeyPress(object sender, KeyPressEventArgs e)
                {

                        e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.');
                
                        
                    }
                
                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                A while ago I wrote this article[^] for a simple numeric text box. It would need some alteration to allow a decimal point and to check the number is valid for your requirements (at the moment it only allows numeric input i.e. it is still a text box and not a NumberBox) but it could serve as a starting point. It handles copy and paste via keyboard/mouse and all edit keys etc...

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                1 Reply Last reply
                0
                • T tonyonlinux

                  I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...

                  private void Price_KeyPress(object sender, KeyPressEventArgs e)
                  {

                          e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.');
                  
                          
                      }
                  
                  O Offline
                  O Offline
                  OkkiePepernoot
                  wrote on last edited by
                  #8

                  Why not using a ConvertEventHandler delegate ?

                  1 Reply Last reply
                  0
                  • T tonyonlinux

                    I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...

                    private void Price_KeyPress(object sender, KeyPressEventArgs e)
                    {

                            e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.');
                    
                            
                        }
                    
                    H Offline
                    H Offline
                    hasarian
                    wrote on last edited by
                    #9

                    You need only to check the text of textbox. My textbox is name tAmount

                    private void tAmount_KeyPress(object sender, KeyPressEventArgs e)
                    {
                    int isNumber = 0;
                    Update();
                    string tmp = Convert.ToString(tAmount.Text);
                    if (tmp.Contains(".") == true)
                    e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber) && (e.KeyChar != '\b');
                    else
                    e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber) && (e.KeyChar != '\b') && (e.KeyChar != '.');

                        }
                    
                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups