visual-studio 数字文本框作为 Visual Studio 工具箱中的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3170744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:11:57  来源:igfitidea点击:

numeric-only textbox as a control in Visual Studio toolbox

.netvisual-studiovisual-studio-2008dotnetnuke

提问by pvaju896

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

我想做一个只有数字的文本框。然后我想将相同的内容添加到 Visual Studio 2008 中的控件工具箱中

I've already built the function to allow only numeric.

我已经构建了只允许数字的函数。

How can I make it available in the toolbox?

如何使其在工具箱中可用?

回答by Serkan Hekimoglu

This is how you can create numeric TextBox:

这是创建数字的方法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);
    }
}

回答by Randhi Rupesh

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

回答by Chandan Kumar

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

嗨,您可以在文本框的 textchanged 事件中执行类似的操作。

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

回答by Bill

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

不要重新发明轮子。下载这个: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.

好的。我对一些三年前的建议投了反对票。目前,我建议查看 jQuery UI 的内容。