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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Insure Numeric Data in TextBox

Insure Numeric Data in TextBox

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 3 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.
  • R Offline
    R Offline
    ronboy
    wrote on last edited by
    #1

    I have a windows form that has four input text boxes. The input for each of these must be integers. Is there a way (e.g. property) that I can set which will only allow the user to enter intergers??? Thanks for any help.

    H H 2 Replies Last reply
    0
    • R ronboy

      I have a windows form that has four input text boxes. The input for each of these must be integers. Is there a way (e.g. property) that I can set which will only allow the user to enter intergers??? Thanks for any help.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Extend the TextBox with your own class (like NumericTextBox, for example) and override either IsInputChar or IsInputKey). There are other ways involving overriding WndProc, but then you start getting into Windows notification messages which aren't necessary when the former two methods already encapsulate such functionality. If you don't want to extend the class (and it's usually a good idea to encapsulate your logic rather than handle each instance of it separately), you could handle the KeyDown event of each TextBox (they could use the same handler, though).

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • R ronboy

        I have a windows form that has four input text boxes. The input for each of these must be integers. Is there a way (e.g. property) that I can set which will only allow the user to enter intergers??? Thanks for any help.

        H Offline
        H Offline
        Huseyin Altindag
        wrote on last edited by
        #3

        Hi Ronboy, There is a way doing that.What you need to do is : -Fire a KeyPress event in properties for your TextBox this.textBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_KeyPress); private void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar !=(char)8) e.Handled=true; } So you can enter now in the TextBox ONLY integers AND Backspace-key with (char)8 . e.KeyChar means it gets the char which is correspending to the key you pressed. e.Handled=true; means if you entered a non-integer value(0-9) you don´t pass this char to control element(TextBox). good coding Bye Huseyin Altindag Huseyin Altindag

        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