C# - 禁用文本框中的键盘输入

发布于 2024-11-25 05:12:05 字数 1871 浏览 0 评论 0原文

我试图禁用输入到文本框中的所有击键,但以下除外:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

遮云壑 2024-12-02 05:12:05

使用 textBox1.KeyPress += textBox1_KeyPress

此代码仅允许数字和 . 以及退格键。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
    { 
        e.Handled = true; 
    }
    //Edit: Alternative
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
    {
        e.Handled = true;
    }
}

Use textBox1.KeyPress += textBox1_KeyPress

This code only allowing numbers and . and the backspace.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
    { 
        e.Handled = true; 
    }
    //Edit: Alternative
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
    {
        e.Handled = true;
    }
}
没有心的人 2024-12-02 05:12:05

您可以尝试通过两种方法来解决此问题:

  1. 等待用户完成输入数据,然后使用 double.TryParse() 确保它是有效数字。

  2. 使用 KeyPress 事件的文本框以在按下每个键时验证数据。

There are two ways you could try to approach this:

  1. Wait for the user to finish entering the data, then use double.TryParse() to make sure it's a valid number.

  2. Use the KeyPress event of the TextBox to validate the data as each key is pressed.

ぃ双果 2024-12-02 05:12:05

看一下这里: ​​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.

↙温凉少女 2024-12-02 05:12:05

您应该挂钩 KeyDownKeyPress 事件以防止输入不需要的字符。 MSDN 上有一个 示例

You should hook into the KeyDown and KeyPress event to prevent the input of unwanted characters. There is a sample on MSDN

水染的天色ゝ 2024-12-02 05:12:05

如果您发现自己使用其中之一,许多第三方控件库也内置了此类功能。

Many third party control libraries also have this kind of functionality built in if you ever find yourself using one of them.

等数载,海棠开 2024-12-02 05:12:05

更好的做法是允许输入任何内容,但通过错误提供程序发出有关验证失败的通知(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.

臻嫒无言 2024-12-02 05:12:05

下面的代码将抑制除数字、退格键和小数点之外的所有内容。它还只允许输入单个小数。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
    { 
        e.Handled = true;
        return; 
    }
    if(e.KeyChar == '.' && textBox1.Text.Contains('.'))
    {
        e.Handled = true;
    }
}

The below code will suppress all but number, the backspace, and the decimal. It also only allows a single decimal for numeric entry.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
    { 
        e.Handled = true;
        return; 
    }
    if(e.KeyChar == '.' && textBox1.Text.Contains('.'))
    {
        e.Handled = true;
    }
}
铜锣湾横着走 2024-12-02 05:12:05

这个也可以用

if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.'){
            e.Handled = true;}

This also can be used

if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.'){
            e.Handled = true;}
_蜘蛛 2024-12-02 05:12:05

使文本/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);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文