C# 仅在文本框中限制输入数字?(C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16871104/
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
Restricting Input to Numbers Only In Textbox? (c#)
提问by Fadzitt
Please excuse me for asking this, I have looked at various different questions relating to this and I still can not get it to implement.
请原谅我提出这个问题,我已经查看了与此相关的各种不同问题,但仍然无法实施。
Using the answers I have looked at, I have gathered this, and applied this coding to my text box.
使用我看过的答案,我收集了这个,并将此编码应用于我的文本框。
private void TxtBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
e.Handled = true;
}
Now, when I proceed to run the program, I am still able to enter letters into it. I do not know what to do next, so any solution would be great. thanks.
现在,当我继续运行该程序时,我仍然可以在其中输入字母。我不知道接下来要做什么,所以任何解决方案都会很棒。谢谢。
采纳答案by Georgi-it
Okay this is what i made just now, it works 100% just tested it.Note that my textBox is named serialTxtBox, you can change it to yours.
好的,这就是我刚刚做的,它 100% 工作,刚刚测试它。注意我的 textBox 被命名为 serialTxtBox,你可以把它改成你的。
void serialTxtBox_TextChanged(object sender, EventArgs e)
{
bool enteredLetter = false;
Queue<char> text = new Queue<char>();
foreach (var ch in this.serialTxtBox.Text)
{
if (char.IsDigit(ch))
{
text.Enqueue(ch);
}
else
{
enteredLetter = true;
}
}
if (enteredLetter)
{
StringBuilder sb = new StringBuilder();
while (text.Count > 0)
{
sb.Append(text.Dequeue());
}
this.serialTxtBox.Text = sb.ToString();
this.serialTxtBox.SelectionStart = this.serialTxtBox.Text.Length;
}
}
EDIT: Definitely you are doing something wrong. In your form constructor which is named like your form. In my case SerialGenerator, you need to initialize the event. In my case :
编辑:你肯定做错了什么。在您的表单构造函数中,它的名称与您的表单相似。在我的 SerialGenerator 中,您需要初始化事件。就我而言:
public SerialGenerator()
{
InitializeComponent();
this.serialTxtBox.TextChanged += serialTxtBox_TextChanged;
}
this will fire the method everytime someone enters something in your textBox. Make sure you rename it to your textbox's name
每次有人在您的文本框中输入内容时,这都会触发该方法。确保将其重命名为文本框的名称
回答by Charlie
Try this one,hope this works
试试这个,希望这有效
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int asciiCode = Convert.ToInt32(e.KeyChar);
if ((asciiCode >= 48 && asciiCode <= 57))
{
}
else
{ MessageBox.Show("Not allowed!");
e.Handled = true;
}
}
回答by Shakti Prakash Singh
You can try this:
你可以试试这个:
private void TxtBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if(!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)))
{ e.Handled = true; }
}
I don't think this is the best one, but you may make this work by tweaking a little, I guess. I don't have VS right now to check what would work.
我不认为这是最好的,但我想你可以通过稍微调整来完成这项工作。我现在没有 VS 来检查什么会起作用。
EDIT: My bad. I think you can use the above one not on keypress but on textchanged event of the textbox. It's more like a tweak than a solution. Just to make you progress if you are stuck and don't get a better solution.
编辑:我的错。我认为你可以在文本框的 textchanged 事件而不是按键上使用上面的一个。这更像是一种调整而不是解决方案。只是为了在您陷入困境并且没有得到更好的解决方案时让您取得进步。
Update: Updated the code. Please check if this one helps you.
更新:更新了代码。请检查这个是否对您有帮助。
回答by Damith
I have added events on code level, since there can be a possibility in your code not correctly event added to the control.
我在代码级别添加了事件,因为您的代码中可能没有正确地将事件添加到控件中。
Note : The KeyPress event is not raised by noncharacter keysyou need to use both KeyDownand KeyPressevents
注意:KeyPress 事件不是由您需要同时使用KeyDown和KeyPress事件的非字符键引发
public partial class Form1 : Form
{
private bool nonNumberEntered = false;
public Form1()
{
InitializeComponent();
textBox1.KeyDown+=new KeyEventHandler(textBox1_KeyDown);
textBox1.KeyPress+=new KeyPressEventHandler(textBox1_KeyPress);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
nonNumberEntered = false;
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
if (e.KeyCode != Keys.Back)
{
nonNumberEntered = true;
}
}
}
if (Control.ModifierKeys == Keys.Shift)
{
nonNumberEntered = true;
}
}
}
REF : Control.KeyPress Event
REF : Control.KeyPress 事件
回答by matzone
Try this ...
尝试这个 ...
private void TxtBox5_KeyPress(object sender, KeyPressEventArgs e)
{
int num = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out num);
}

