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 set TextBox to only accept numbers?

How to set TextBox to only accept numbers?

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 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.
  • N Offline
    N Offline
    naouf10
    wrote on last edited by
    #1

    I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you

    L G B 3 Replies Last reply
    0
    • N naouf10

      I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use the .TextChanged event as another avenue for filtering / altering text box input.

      1 Reply Last reply
      0
      • N naouf10

        I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you

        G Offline
        G Offline
        Gilbert Consellado
        wrote on last edited by
        #3

        You can use TextChanged event as Gerry said. Then you can verify the text using this method.

        public static bool IsNumber(string s)
        {
        var r = new Regex(@"^-?[0-9]\d*(\.\d+)?$");
        return r.Match(s).Success;
        }

        BTW it also accept negative numbers

        1 Reply Last reply
        0
        • N naouf10

          I have already checked other questions here but the answers are not related to my issue. the following code allows textbox1 to only accept numbers if the physical keyboard (laptop) is pressed: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char ch = e.KeyChar; if ( !char.IsDigit(ch)) { e.Handled = true; } } but this is not what I wanted (I dont use physical laptop keyboard). I have windows form with buttons and a textbox1. I designed keyboard and it works well but I want textbox1 to only accept numbers and the ".". There are only two lines of code inside each button (and only code in the project) which is: private void buttonName_Click(object sender, EventArgs e) { // each button only has this code. textBox1.Focus(); SendKeys.Send(buttonName.Text); } I know how to set textbox to accept numbers if the physical (laptop ) keys are pressed but here in this case I have windows form with control buttons ( each button to print its text into textbox1) and I want to set textBox1 to only accept numbers and the ".". Please help in how to achieve this. Thank you

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Assuming this is Windows Forms, and there's no physical keyboard:

          private void NumberButtons_Click(object sender, EventArgs e)
          {
          textBox1.AppendText((sender as Button).Text);
          }

          The assumption here is that you have a set of Buttons whose Text values are from #1~9 and #0, and they all use the same Click EventHandler. Of course, we don't know here exactly what you mean by "numbers:" are you allowing decimal-point, commas, negative numbers, culture-specific numeric encoding, etc. ? Will also have a back-space button ? If you wish the user to have some way to move the insertion point around in the TextBox, that'll take a bit more work (using virtual arrow-key buttons ?).

          «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

          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