numeric-only textbox as a control in Visual Studio toolbox

倖福魔咒の 提交于 2019-12-17 16:26:38

问题


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 allow only numeric.

How can I make it available in the toolbox?


回答1:


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);
    }
}



回答2:


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;
  }



回答3:


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;
    }



回答4:


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.



来源:https://stackoverflow.com/questions/3170744/numeric-only-textbox-as-a-control-in-visual-studio-toolbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!