Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# - Fatal编程技术网

C# 如何停止键盘键的默认功能?

C# 如何停止键盘键的默认功能?,c#,C#,我创建了一个TextBox,并绑定空格以检查textbox1和richtextbox1中的字符串是否相同: if (e.KeyCode == Keys.Space) { if (richTextBox1.Text.Contains(textBox1.Text)) { richTextBox1.Text = richTextBox1.Text.Replace(textBox1.Text + " ", ""); wpm++; textB

我创建了一个
TextBox
,并绑定空格以检查
textbox1
richtextbox1
中的字符串是否相同:

if (e.KeyCode == Keys.Space)
{
    if (richTextBox1.Text.Contains(textBox1.Text))
    {
        richTextBox1.Text = richTextBox1.Text.Replace(textBox1.Text + " ", "");
        wpm++;
        textBox1.Text = "";
    }
}

因此,我希望在按下空格键时,不要在
textbox1

中写入空格。假设您使用的是Windows窗体,您可以使用重写任何键行为。

此答案改编自MSDN示例


你的代码有问题吗?你有问题吗?有了这个代码没有,但我想知道我如何才能做到这一点E.SuppressKeyPress=true;你能确切地说明你想做什么吗?似乎您希望
空格
键在输入特定控件时表示某些内容。你发了一些代码,但你说没有问题。对我来说,你的问题不清楚。
// Boolean flag used to determine when a character other than a space is entered
private bool spaceEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    spaceEntered = e.KeyCode == Keys.Space;
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (spaceEntered == true)
    {
        // Stop the character from being entered into the control since 
        e.Handled = true;
    }
}