C# 如何禁用一个键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/644071/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:32:28  来源:igfitidea点击:

C# How do disable a key

c#eventskey

提问by

How do I prevent the caret going to the next line in a text box when the 'ENTER' key has been pressed? In other words how to disable the 'ENTER' or 'RETURN' key in a text-box?

按下“ENTER”键后,如何防止插入符号转到文本框中的下一行?换句话说,如何禁用文本框中的“ENTER”或“RETURN”键?

回答by Jedi Master Spooky

You can write the OnKeyDownevent. you can use the e.SuppressKeyPressto tell .NET that you handle the key. Something like this:

您可以编写OnKeyDown事件。您可以使用e.SuppressKeyPress告诉 .NET 您处理密钥。像这样的东西:

if (e.KeyCode == Keys.Enter) {
    e.SuppressKeyPress = true;
}

回答by Jakob Christensen

Take a look at TextBox.AcceptsReturn.

看看TextBox.AcceptsReturn