C# - 禁用文本框中的键盘输入
我试图禁用输入到文本框中的所有击键,但以下除外:
0 1 2 3 4 5 6 7 8 9 。 (因此除了数字和“.”之外的所有键都应该被禁用)
现在我有以下代码,但它只检查是否输入了字母作为第一个值(更不用说它真的很草率):
private void yDisplacementTextBox_TextChanged(object sender, EventArgs e)
{
if (yDisplacementTextBox.Text.ToUpper() == "A" || yDisplacementTextBox.Text.ToUpper() == "B" || yDisplacementTextBox.Text.ToUpper() == "C" ||
yDisplacementTextBox.Text.ToUpper() == "D" || yDisplacementTextBox.Text.ToUpper() == "E" || yDisplacementTextBox.Text.ToUpper() == "F" ||
yDisplacementTextBox.Text.ToUpper() == "G" || yDisplacementTextBox.Text.ToUpper() == "H" || yDisplacementTextBox.Text.ToUpper() == "I" ||
yDisplacementTextBox.Text.ToUpper() == "J" || yDisplacementTextBox.Text.ToUpper() == "K" || yDisplacementTextBox.Text.ToUpper() == "L" ||
yDisplacementTextBox.Text.ToUpper() == "M" || yDisplacementTextBox.Text.ToUpper() == "N" || yDisplacementTextBox.Text.ToUpper() == "O" ||
yDisplacementTextBox.Text.ToUpper() == "P" || yDisplacementTextBox.Text.ToUpper() == "Q" || yDisplacementTextBox.Text.ToUpper() == "R" ||
yDisplacementTextBox.Text.ToUpper() == "S" || yDisplacementTextBox.Text.ToUpper() == "T" || yDisplacementTextBox.Text.ToUpper() == "U" ||
yDisplacementTextBox.Text.ToUpper() == "V" || yDisplacementTextBox.Text.ToUpper() == "W" || yDisplacementTextBox.Text.ToUpper() == "X" ||
yDisplacementTextBox.Text.ToUpper() == "Y" || yDisplacementTextBox.Text.ToUpper() == "Z")
{
MessageBox.Show("Please enter a numeric value for the Y Displacement.", "Y Displacement: Numbers Only Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
是否有办法让按下时,键盘上的所有键(数字和句点按钮除外)都不会注册(或禁用)该键的实际值并没有输入任何内容?
I am trying to disable all keystrokes entered into a text box except the following:
0 1 2 3 4 5 6 7 8 9 . (so all keys except the numbers and the '.' should be disabled)
Right now I have the following code but it only checks to see if a letter was entered as the first value (not to mention its really sloppy):
private void yDisplacementTextBox_TextChanged(object sender, EventArgs e)
{
if (yDisplacementTextBox.Text.ToUpper() == "A" || yDisplacementTextBox.Text.ToUpper() == "B" || yDisplacementTextBox.Text.ToUpper() == "C" ||
yDisplacementTextBox.Text.ToUpper() == "D" || yDisplacementTextBox.Text.ToUpper() == "E" || yDisplacementTextBox.Text.ToUpper() == "F" ||
yDisplacementTextBox.Text.ToUpper() == "G" || yDisplacementTextBox.Text.ToUpper() == "H" || yDisplacementTextBox.Text.ToUpper() == "I" ||
yDisplacementTextBox.Text.ToUpper() == "J" || yDisplacementTextBox.Text.ToUpper() == "K" || yDisplacementTextBox.Text.ToUpper() == "L" ||
yDisplacementTextBox.Text.ToUpper() == "M" || yDisplacementTextBox.Text.ToUpper() == "N" || yDisplacementTextBox.Text.ToUpper() == "O" ||
yDisplacementTextBox.Text.ToUpper() == "P" || yDisplacementTextBox.Text.ToUpper() == "Q" || yDisplacementTextBox.Text.ToUpper() == "R" ||
yDisplacementTextBox.Text.ToUpper() == "S" || yDisplacementTextBox.Text.ToUpper() == "T" || yDisplacementTextBox.Text.ToUpper() == "U" ||
yDisplacementTextBox.Text.ToUpper() == "V" || yDisplacementTextBox.Text.ToUpper() == "W" || yDisplacementTextBox.Text.ToUpper() == "X" ||
yDisplacementTextBox.Text.ToUpper() == "Y" || yDisplacementTextBox.Text.ToUpper() == "Z")
{
MessageBox.Show("Please enter a numeric value for the Y Displacement.", "Y Displacement: Numbers Only Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Is there anyway to have it so when pressed, all of the keys on the keyboard (except the numbers and the period button) do not register (or disables) the actual value of the key and inputs nothing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用
textBox1.KeyPress += textBox1_KeyPress
此代码仅允许数字和
.
以及退格键。Use
textBox1.KeyPress += textBox1_KeyPress
This code only allowing numbers and
.
and the backspace.您可以尝试通过两种方法来解决此问题:
等待用户完成输入数据,然后使用
double.TryParse()
确保它是有效数字。使用 KeyPress 事件的文本框以在按下每个键时验证数据。
There are two ways you could try to approach this:
Wait for the user to finish entering the data, then use
double.TryParse()
to make sure it's a valid number.Use the KeyPress event of the TextBox to validate the data as each key is pressed.
看一下这里: Simple Numeric TextBox
编辑:正如其他答案解释了该怎么做处理 OnKeyPress/OnKeyDown 事件,本文演示了如何处理其他场景,如粘贴文本,所以我将保留这个答案。
Take a look here: Simple Numeric TextBox
EDIT: As other answers explains what to do dealing with OnKeyPress/OnKeyDown events, this article demonstrates how to deal with other scenarios, as pasting text, so I'll keep this answer.
您应该挂钩
KeyDown
和KeyPress
事件以防止输入不需要的字符。 MSDN 上有一个 示例You should hook into the
KeyDown
andKeyPress
event to prevent the input of unwanted characters. There is a sample on MSDN如果您发现自己使用其中之一,许多第三方控件库也内置了此类功能。
Many third party control libraries also have this kind of functionality built in if you ever find yourself using one of them.
更好的做法是允许输入任何内容,但通过错误提供程序发出有关验证失败的通知(Validating 事件和 double.TryParse())。
但如果您坚持,请务必替换“.”。与系统的小数点分隔符。如果您不假设所有值都是英文,您很容易陷入无法输入十进制值的问题。
例如,我在克罗地亚,这里我们必须输入逗号(,)。在一些伊斯兰国家,我不记得小数点分隔符是井号(#)。
所以要小心本地化问题。
It is better practice to allow typing anything but give notification through an error provider about the failing validation (Validating event and double.TryParse()).
But if you insist, be sure to replace the '.' with the decimal separator of the system. If you are not assuming all values to be English you can easily get into a cannot-enter-a-decimal-value problem.
For instance, I am in Croatia and here we have to type a comma (,). In some islamic country I fail to remember the decimal separator is a hash (#).
So beware of localization issues.
下面的代码将抑制除数字、退格键和小数点之外的所有内容。它还只允许输入单个小数。
The below code will suppress all but number, the backspace, and the decimal. It also only allows a single decimal for numeric entry.
这个也可以用
This also can be used
使文本/cmb框只读的最短修复,将以下内容附加到相应的按键事件:
private void cmbDisable_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (char)(0);
}
Shortest fix to make text/cmb box read-only, attach following to respective keypress event :
private void cmbDisable_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (char)(0);
}