Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止用户键入除箭头键以外的所有字符?_C#_Windows Mobile - Fatal编程技术网

C# 如何防止用户键入除箭头键以外的所有字符?

C# 如何防止用户键入除箭头键以外的所有字符?,c#,windows-mobile,C#,Windows Mobile,我的WinForm程序中有文本框 如何防止用户键入除箭头键、Esc和Enter之外的所有字符 对不起,我忘了写这封信是为Windows mobile写的,在Windows mobile中没有 e.SuppressKeyPress = true; 谢谢,很抱歉没有理解您可以处理事件文本框。按键 存在您不想传递到文本框的筛选键-检查KeyEventArgs.KeyCode是否为您的代码) 然后将KeyEventArgs.Handled和KeyEventArgs.SuppressKeyPress设置

我的WinForm程序中有文本框

如何防止用户键入除箭头键、Esc和Enter之外的所有字符

对不起,我忘了写这封信是为Windows mobile写的,在Windows mobile中没有

e.SuppressKeyPress = true;

谢谢,很抱歉没有理解

您可以处理事件
文本框。按键

存在您不想传递到文本框的筛选键-检查KeyEventArgs.KeyCode是否为您的代码)


然后将KeyEventArgs.Handled和KeyEventArgs.SuppressKeyPress设置为true。

由于BoltClock写入的字符不可打印,因此您无法“键入”它们。如果要忽略这些字符并对其他字符执行其他操作,则可以在文本框的
向下键
事件中执行此操作

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (
            e.KeyCode == Keys.Left || 
            e.KeyCode == Keys.Right ||
            e.KeyCode == Keys.Down ||
            e.KeyCode == Keys.Up ||
            e.KeyCode == Keys.Enter || 
            e.KeyCode == Keys.Escape
            )
        {
            e.SuppressKeyPress = true;
            return;
        }
        //do sth...
    }

KeyDown事件将为您执行此操作

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            // These keys will be allowed
            case Keys.Left:
            case Keys.Right:
            case Keys.Up:
            case Keys.Down:
            case Keys.Escape:
            case Keys.Enter:
                break;

            // These keys will not be allowed
            default:
                e.SuppressKeyPress = true;
                break;
        }
    }

但这些不是可打印的字符键。。。您的意思是不希望文本可编辑吗?如果跳过
e.SuppressKeyPress=true
,会发生什么情况?Handled属性实际上不会这样做。请改用e.SuppressKeyPress属性。