numeric-only textbox as a control in Visual Studio toolbox

前端 未结 4 1404
情深已故
情深已故 2020-12-06 03:43

I would like to make one numeric-only textbox. I\'d like to then add that same to the control toolbox within Visual Studio 2008

I\'ve already built the function to a

相关标签:
4条回答
  • 2020-12-06 04:05

    Hi you can do something like this in the textchanged event of the textbox.

    here is a demo

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string actualdata = string.Empty;
            char[] entereddata = textBox1.Text.ToCharArray();
            foreach (char aChar in entereddata.AsEnumerable())
            {
                if (Char.IsDigit(aChar))
                {
                    actualdata = actualdata + aChar;
                    // MessageBox.Show(aChar.ToString());
                }
                else
                {
                    MessageBox.Show(aChar + " is not numeric");
                    actualdata.Replace(aChar, ' ');
                    actualdata.Trim();
                }
            }
            textBox1.Text = actualdata;
        }
    
    0 讨论(0)
  • 2020-12-06 04:15

    Don't reinvent the wheel. Download this:
    http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/NumericUpDown/NumericUpDown.aspx

    EDIT:

    Okay. I am getting downvoted for some three-year old advice. Currently, I would recommend looking into the contents of jQuery UI.

    0 讨论(0)
  • 2020-12-06 04:19

    This is how you can create numeric TextBox:

    public class NumericTextBox : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            base.OnKeyPress(e);
        }
    }
    
    0 讨论(0)
  • 2020-12-06 04:25

    Call this method on key press

      function NumberOnly(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
    
         return true;
      }
    
    0 讨论(0)
提交回复
热议问题