Text box with numeric text as a control in the Visual Studio toolbar

I would like to make one text box with a numeric value. I would like to add the same to the control toolbar in Visual Studio 2008

I have already built a function to allow only numeric ones.

How can I make it available in the toolbar?

+3
source share
4 answers

Here's 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);
    }
}
+6
source
0

Call this method when a key is pressed.

  function NumberOnly(evt)
  {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
  }
0
source

Hi, you can do something like this in a textchanged event of a text field.

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
source

Source: https://habr.com/ru/post/1752957/


All Articles