C# 数字文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/508533/
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-08-04 06:11:26  来源:igfitidea点击:

Numeric TextBox

c#textboxnumeric

提问by Ricardo

Im new to programming and I dont know very much about but I'm making a calculator, and i want to use a textbox that only acepts numbers and decimals, and when the user paste text from the clipboard the textbox deletes any literal characters, like the MS calc.

我是编程新手,我不太了解,但我正在制作一个计算器,我想使用一个只接受数字和小数的文本框,当用户从剪贴板粘贴文本时,文本框会删除任何文字字符,例如MS 计算。

Please take the time to explain each part so I can learn or write it and tell me what to search.

请花时间解释每个部分,以便我可以学习或编写它并告诉我要搜索的内容。

Thanks

谢谢

EDIT: I'll make it more specific:

编辑:我会让它更具体:

How can I make a numeric textbox in C#? I've used the masked textbox but it wont take decimals.

如何在 C# 中制作数字文本框?我使用了掩码文本框,但它不会带小数。

I've read things about overloading the OnKeyPress method so it will correct any wrong characters but I dont know to do it.

我已经阅读了有关重载 OnKeyPress 方法的内容,因此它会更正任何错误的字符,但我不知道该怎么做。

采纳答案by Timur Fanshteyn

The easiest way :)

最简单的方法:)

on Keypress event on your textbox

文本框上的 Keypress 事件



if ((e.KeyChar <= 57 && e.KeyChar >= 48) || e.KeyChar == 13 || e.KeyChar == 8)
{
}
else
{
     e.Handled = true;
}


回答by Fredou

here how to do this in vb.net

这里如何在 vb.net 中做到这一点

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    Dim reg As New System.Text.RegularExpressions.Regex("[^0-9_ ]")
    TextBox1.Text = reg.Replace(TextBox1.Text, "")
End Sub

just fix the regex for your specific needs

只需根据您的特定需求修复正则表达式

回答by Austin Salonen

Research the MaskedTextBox.

研究 MaskedTextBox。

The question is a little broad to explain everything. Try to focus the question if you want specifics because you're asking for a lot of the community to "explain each part." If you ask a few specific questions (and exclude the "please the the time to explain..."), you'll get better responses.

这个问题有点宽泛,无法解释一切。如果您想要细节,请尝试集中问题,因为您要求很多社区“解释每个部分”。如果你问一些具体的问题(并排除“请花时间解释……”),你会得到更好的回答。

回答by Simon

As far as I am aware there's nothing native in the .NET framework (2.0 at least) to do this. Your options would be:

据我所知,.NET 框架(至少 2.0)中没有任何本机可以做到这一点。您的选择是:

  1. Create a custom control which inherits from the textbox control and only allows numeric input. This has the advantage that the control can be reused.
  2. Handle the KeyPress event and check the charCode to only allow numeric keystrokes. This is easier but much less reusable.
  1. 创建一个继承自文本框控件且仅允许数字输入的自定义控件。这具有可以重复使用控件的优点。
  2. 处理 KeyPress 事件并检查 charCode 以仅允许数字键击。这更容易,但可重用性要低得多。

回答by Brian Sweeney

i would probably use a regular expression to screen out non-numerics.

我可能会使用正则表达式来筛选出非数字。

pseudo code:

伪代码:

for (each item in the input string) {
   if (!match(some regular expression, item)) {
        toss it out
   } else {
        add item to text box or whatever you were going to do with it
   }

}

回答by Timur Fanshteyn

If you look closely, In Windows Calculator, the numbers are shown in a label not a textbox (It does not receive focus). The window receives keyboard events.

如果仔细观察,在 Windows 计算器中,数字显示在标签中而不是文本框中(它没有获得焦点)。窗口接收键盘事件。

So look at KeyPressed and KeyDown events on the form.

所以看看表单上的 KeyPressed 和 KeyDown 事件。

回答by Albert

You could use a plain textbox or label as the calculator display and just make sure the value (a string?) is always a number. For instance, you could keep a double and convert it to a string when you wish to display it.

您可以使用纯文本框或标签作为计算器显示,并确保值(字符串?)始终是数字。例如,您可以保留一个 double 并在您希望显示它时将其转换为一个字符串。

回答by Nathan

Being a novice you might be better off investing in a good third party toolkit. Radcontrols from Telerik for instance has a numeric textbox that will accomplish what you are looking for.

作为新手,您最好投资一个好的第三方工具包。例如,来自 Telerik 的 Radcontrols 有一个数字文本框,可以完成您正在寻找的内容。

回答by Guulh

There is a control in the framework which is specially made for numeric input : the NumericUpDown control. It also manages decimal values.

框架中有一个专门用于数字输入的控件:NumericUpDown 控件。它还管理十进制值。

回答by Guulh

Add an event handler for the textbox you want to be numeric only, and add the following code:

为您希望仅为数字的文本框添加事件处理程序,并添加以下代码:

private void textBoxNumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
{
   if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
   {
      e.Handled = false;
   }
   else
   {
      e.Handled = true;
   }
}

This allows for numbers 0 to 9, and also backspaces (useful IMHO). Allow through the '.' character if you want to support decimals

这允许数字 0 到 9,以及退格(有用的恕我直言)。允许通过 '.' 字符,如果你想支持小数