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. How to make a Textbox only take numbers?

How to make a Textbox only take numbers?

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
5 Posts 4 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.
  • D Offline
    D Offline
    dipuks
    wrote on last edited by
    #1

    I made a C# application and i want the Textbox control to just take numbers and no characters. In VC++ this can be easily done by setting a property. Any idea how to do this in C#? Thanks

    G S R 3 Replies Last reply
    0
    • D dipuks

      I made a C# application and i want the Textbox control to just take numbers and no characters. In VC++ this can be easily done by setting a property. Any idea how to do this in C#? Thanks

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Handle the keypress event and check input

      my articles

      1 Reply Last reply
      0
      • D dipuks

        I made a C# application and i want the Textbox control to just take numbers and no characters. In VC++ this can be easily done by setting a property. Any idea how to do this in C#? Thanks

        S Offline
        S Offline
        sxbluebird
        wrote on last edited by
        #3

        this is easy thing. you can set as : this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ( !( char.IsDigit( e.KeyChar ) ) ) { e.Handled = true; } }

        roger wong msn:wenmianbj@hotmail.com

        D 1 Reply Last reply
        0
        • D dipuks

          I made a C# application and i want the Textbox control to just take numbers and no characters. In VC++ this can be easily done by setting a property. Any idea how to do this in C#? Thanks

          R Offline
          R Offline
          Revathij
          wrote on last edited by
          #4

          U can use Regular Expression Validation control. Validation Control will not allow other characters except numbers by setting the expression as ^[0-9]+$ in validation control

          1 Reply Last reply
          0
          • S sxbluebird

            this is easy thing. you can set as : this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ( !( char.IsDigit( e.KeyChar ) ) ) { e.Handled = true; } }

            roger wong msn:wenmianbj@hotmail.com

            D Offline
            D Offline
            dipuks
            wrote on last edited by
            #5

            The method you suggested works. Thanks. One problem now is that the "Backspace" button no more works inside the text box. Any idea why thats happening? "Delete" button works.

            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