C# 停止按钮获得焦点点击 [英] C# Stop Button From Gaining Focus on Click
问题描述
我有几个按钮,当点击它们时,我不希望它们获得焦点,也不希望空格键再次按下"它们.
I have several buttons that when clicked I don't want them to get focus nor do I want the space bar to 'press' them again.
我想要与 Windows 计算器中的按钮相同的功能.
I want the same functionality as the buttons in windows calculator.
谷歌搜索和搜索堆栈一切似乎都与表单有关,例如.使表单在 C# 中不可聚焦
Googled and searched stack everything seems to be about forms eg. Make a form not focusable in C#
我知道我应该重写 WndProc 但不完全确定如何处理我应该捕获/忽略的消息等.据我所知:
I know I'm supposed to rewrite WndProc but not exactly sure how to proceed as to what messages I should catch/ignore etc. As far as I got:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}
推荐答案
您所要做的就是将此行添加到键的 Click
事件的末尾:
All you got to do is add this line to the end of the key's Click
event:
this.Focus();
这一行会导致按钮失去焦点,表单获得焦点,空格键无效,从而满足你的2个条件.
This line will cause the button to lose focus, the form will gain focus and spacebar will have no effect, thus satisfying your 2 conditions.
现在,如果您不想再次单击该按钮,请改为添加以下两行:
Now if you don't want the button to be able to be clicked again, then add these 2 lines instead:
this.Focus();
((Button)sender).Enabled = false;
这将执行另一行所做的操作,此外,它将禁用按钮.
This will do what the other line did and in addition, it will disable the button.
这篇关于C# 停止按钮获得焦点点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!