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 an number only text box?

How to make an number only text box?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
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.
  • M Offline
    M Offline
    Ming Luo
    wrote on last edited by
    #1

    Hi all: I am doing a windows form application. I need to some textboxes which can only be inserted numbers like prices. Is there any existing component in windows application of C# please? What's the best way of achieving this please? Thanks alot

    Asura

    L 3 2 Replies Last reply
    0
    • M Ming Luo

      Hi all: I am doing a windows form application. I need to some textboxes which can only be inserted numbers like prices. Is there any existing component in windows application of C# please? What's the best way of achieving this please? Thanks alot

      Asura

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

      Process the KeyDown event and check, if it's a number:

      try
      {
      int number = int.Parse(textbox.Text);
      }
      catch (Exception e)
      {
      // text is not a number
      }

      You could also use a MaskedEditBox (or how that is called, I haven't used this one yet) regards

      1 Reply Last reply
      0
      • M Ming Luo

        Hi all: I am doing a windows form application. I need to some textboxes which can only be inserted numbers like prices. Is there any existing component in windows application of C# please? What's the best way of achieving this please? Thanks alot

        Asura

        3 Offline
        3 Offline
        3Dizard
        wrote on last edited by
        #3

        Greeeg is definitely right, but I'd prefer a slightly different way than using exception. Here's the code: private void AmountBox_KeyPress(object sender, KeyPressEventArgs e) { switch (e.KeyChar) { case '0': break; case '1': break; case '2': break; case '3': break; case '4': break; case '5': break; case '6': break; case '7': break; case '8': break; case '9': break; default: e.Handled = true; break; } } Setting Handled to true prevents the input from being processed further and that is exactly what you want for anything else than numbers. BTW: This is a function for the KeyPress event. Greetings

        M 1 Reply Last reply
        0
        • 3 3Dizard

          Greeeg is definitely right, but I'd prefer a slightly different way than using exception. Here's the code: private void AmountBox_KeyPress(object sender, KeyPressEventArgs e) { switch (e.KeyChar) { case '0': break; case '1': break; case '2': break; case '3': break; case '4': break; case '5': break; case '6': break; case '7': break; case '8': break; case '9': break; default: e.Handled = true; break; } } Setting Handled to true prevents the input from being processed further and that is exactly what you want for anything else than numbers. BTW: This is a function for the KeyPress event. Greetings

          M Offline
          M Offline
          Martin23
          wrote on last edited by
          #4

          This is a better way still. Using execeptions is not a good idea. private void AmountBox_KeyPress(object sender, KeyPressEventArgs e) { if(!char.IsNumber(e.KeyChar)) e.Handled = true; }

          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