locked
What is the best way to create an Amount field? RRS feed

  • Question

  • I have to create an amount field and a percentage field on a form.

    What is the best way to do so? With a TextBox and editing???

    Thanks for your review and am hopeful for a reply.

    PSULionRP

    Friday, May 10, 2013 4:35 PM

Answers

  • Two options:

    1. Use a NumericUpDown instead. NumericUpDown does the filtering for you, which is nice. Of course it also gives your users the ability to hit the up and down arrows on the keyboard to increment and decrement the current value.

    2. Handle the appropriate keyboard events to prevent anything but numeric input. I've had success with this two event handlers on a standard TextBox:

      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (!char.IsControl(e.KeyChar) 
              && !char.IsDigit(e.KeyChar) 
              && e.KeyChar != '.')
          {
              e.Handled = true;
          }
      
          // only allow one decimal point
          if (e.KeyChar == '.' 
              && (sender as TextBox).Text.IndexOf('.') > -1)
          {
              e.Handled = true;
          }
      }

    You can remove the check for '.' (and the subsequent check for more than one '.') if your TextBox shouldn't allow decimal places. You could also add a check for '-' if your TextBox should allow negative values.

    this is matt hamilton answer on stakeoverflow

    --------------------------------------------------------------------------------------

    i like to use jquery for numbers only input check this out link

    yours,

    OQ 


    Please mark the right post as answer to your question , and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    • Proposed as answer by Omar Qaddoumi Tuesday, May 28, 2013 6:46 AM
    • Marked as answer by Bob Shen Friday, May 31, 2013 10:07 AM
    Friday, May 10, 2013 4:47 PM