[英]Only numbers in the text box
if (!(char.IsDigit(e.KeyChar)))
{
e.Handled = true;
}
The above code is not working properly 上面的代码无法正常工作
Below is the image error : 下面是图像错误:
The problem space is "Clipboard" 问题空间是“剪贴板”
If this is for WinForms, my suggestion would be to use a MaskedTextBox
instead. 如果这是WinForms,我的建议是改用
MaskedTextBox
。 This is a purpose-built control for allowing only certain kinds of user-input. 这是一个专用控件,仅允许某些类型的用户输入。
You can set the mask through the designer or in code. 您可以通过设计器或代码设置蒙版。 For example, for a 5-digit numeric:
例如,对于5位数字:
maskedTextBox1.Mask = "00000";
maskedTextBox1.ValidatingType = typeof(int);
Yes, this is the typical nemesis for keyboard filtering. 是的,这是键盘过滤的典型克星。 The TextBox control doesn't have any built-in events to intercept a paste from the clipboard.
TextBox控件没有任何内置事件来拦截剪贴板中的粘贴。 You'll have to detect the Ctrl+V keypress yourself and screen Clipboard.GetText().
您必须自己检测Ctrl + V并检查Clipboard.GetText()。
The logic is tricky to get right. 逻辑很难正确。 Here's a class that can make all this a little easier.
这是一个可以使所有这些变得更容易的类。 Add a new class to your project and paste the code shown below.
将新类添加到您的项目中,然后粘贴以下代码。 Compile.
编译。 Drop the new control from the top of the toolbox onto a form.
将新控件从工具箱的顶部拖放到窗体上。 Double click it and write the ValidateChar event handler.
双击它并编写ValidateChar事件处理程序。 Like this one, only allowing entering digits:
像这样,只允许输入数字:
private void validatingTextBox1_ValidateChar(object sender, ValidateCharArgs e) {
if (!"0123456789".Contains(e.KeyChar)) e.Cancel = true;
}
The code: 编码:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
[DefaultEvent("ValidateChar")]
class ValidatingTextBox : TextBox {
public event EventHandler<ValidateCharArgs> ValidateChar;
protected virtual void OnValidateChar(ValidateCharArgs e) {
var handler = ValidateChar;
if (handler != null) handler(this, e);
}
protected override void OnKeyPress(KeyPressEventArgs e) {
if (e.KeyChar >= ' ') { // Allow the control keys to work as normal
var args = new ValidateCharArgs(e.KeyChar);
OnValidateChar(args);
if (args.Cancel) {
e.Handled = true;
return;
}
}
base.OnKeyPress(e);
}
private void HandlePaste() {
if (!Clipboard.ContainsText()) return;
string text = Clipboard.GetText();
var toPaste = new StringBuilder(text.Length);
foreach (char ch in text.ToCharArray()) {
var args = new ValidateCharArgs(ch);
OnValidateChar(args);
if (!args.Cancel) toPaste.Append(ch);
}
if (toPaste.Length != 0) {
Clipboard.SetText(toPaste.ToString());
this.Paste();
}
}
bool pasting;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x302 && !pasting) {
pasting = true;
HandlePaste();
pasting = false;
}
else base.WndProc(ref m);
}
}
class ValidateCharArgs : EventArgs {
public ValidateCharArgs(char ch) { Cancel = false; KeyChar = ch; }
public bool Cancel { get; set; }
public char KeyChar { get; set; }
}
Handle TextChanged event or use a MaskedTextBox. 处理TextChanged事件或使用MaskedTextBox。
if (textBox1.Text.Count(a => !char.IsDigit(a)) > 0)
{
textBox1.Text = new string(textBox1.Text.Where(a => char.IsDigit(a)).ToArray());
}
I answered a similar question on StackOverflow once. 我曾经在StackOverflow上回答过类似的问题。
Here's the link to the question: Best way to limit textbox decimal input in c# 这是问题的链接: 限制C#中文本框十进制输入的最佳方法
Essentially, you'll have to put my class in your code and apply it to all textboxes you want to restrict data entered. 本质上,您必须将我的课程放在代码中,并将其应用于要限制输入数据的所有文本框。
The TextBoxFilter class I wrote allows you to limit entry to Alphabet , Numerics , AlphaNumerics , Currency and UserSpecified input. 我编写的TextBoxFilter类允许您将输入限制为Alphabet , Numerics , AlphaNumerics , Currency和UserSpecified输入。
control.TextChanged += (s, a) => {
string value = string.Empty;
foreach (char ch in control.Text.ToCharArray())
{
if (char.IsDigit(ch))
{
value += ch.ToString();
}
}
control.Text = value;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.