简体   繁体   English

我想让表单上的所有文本框都接受仅包含尽可能少的代码的数字-C#

[英]I want make all textboxes on my form to accept only numbers with as little code as possible - c#

I am making a relatively simple bit of software which has quite a few textboxes and i want a way to only allow numbers in all textboxes in the form with hopefully one piece of code. 我正在制作一个相对简单的软件,该软件具有相当多的文本框,并且我希望有一种方法可以只用一段代码允许所有文本框中的数字以该形式存在。 I am currently using the following code to only allow numbers for one textbox but it means repeating those lines for each textbox. 我目前正在使用以下代码,仅允许一个文本框使用数字,但这意味着对每个文本框重复这些行。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

The textboxes are also inside a panel incase that makes a difference. 文本框也位于面板内,以防有所作为。

使用实现的方法创建派生的DigitsOnlyTextBox,并使用它代替TextBoxes。

As Darek said, one viable option is to: 正如Darek所说,一个可行的选择是:

Create a derrived DigitsOnlyTextBox, with the method implemented, and use it in place of TextBoxes 创建派生的DigitsOnlyTextBox,并实现该方法,并使用它代替TextBoxes

A second option is to simply point each TextBox to the same event handler. 第二个选择是简单地将每个TextBox指向相同的事件处理程序。 For example, in the Form.Designer.cs : 例如,在Form.Designer.cs

this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxNumericOnly_KeyPress);
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxNumericOnly_KeyPress);
this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBoxNumericOnly_KeyPress);
...

Then your handler: 然后您的处理程序:

private void textBoxNumericOnly_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

Assuming you're using Visual Studios and you've already created the event handler the first time (for textBox1 ), you can easily do this all in the designer view of the Form : 假设您正在使用Visual Studios,并且已经第一次创建了事件处理程序(针对textBox1 ),则可以在Form的设计器视图中轻松完成所有操作:

A Default Handler 默认处理程序 在VS中添加默认事件处理程序

Copy Def Handler 复制Def处理程序 在VS中添加复制的事件处理程序

If you're using Windows Forms and not WPF you can use a MaskedTextBox . 如果您使用的是Windows窗体而不是WPF,则可以使用MaskedTextBox

Not sure if you can replicate the capability in WPF, having never used it. 不知道您是否可以在WPF中复制该功能,而从未使用过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#在表单中添加所有文本框 - C# Add all TextBoxes in a Form 我可以让我的代码不接受除 int C# 以外的任何输入吗 - Can I make my code not accept any input other than an int C# C#获取斐波那契IEnumerable <int> 与递归..我希望它返回所有数字,而不仅仅是最后一个 - C# get fibonacci IEnumerable<int> with recursion.. I want it to return all the numbers, not only the last one 每次我按下按钮时,我希望它指示 C# 中我的文本框或文本框何时为空 - Evertime I press a button I want it to indicate when my textbox or textboxes are empty in C# Xamarin / PHP,我想用C#(Xamarin)或我的PHP代码将Jarray变成JObject - Xamarin/PHP, I want to make my Jarray to a JObject, either in C# (Xamarin) or in my PHP-code 如何显示我的随机 c# 表单的所有数字 - How to display all the numbers of my random c# form 删除 C# 表单中的文本框只删除了 foreach 中的一半 - Deleting Textboxes in a C# Form deletes only half in foreach 以编程方式将随机数字放入C#中所有形式的文本框中 - Programmatically place random digits into all textboxes of form in c# 我的脚本 (c#) 需要一些帮助,只需几行代码或对数学公式的解释 - I need some help at my script (c#) with little lines of code or explanation for math formula 我想在我的C#应用​​程序中进行以下curl调用 - i want to make the following curl call in my C# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM