C#停止按键从上点击获得焦点 [英] C# Stop Button From Gaining Focus on Click

查看:537
本文介绍了C#停止按键从上点击获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个按钮,点击时,我不希望他们获得焦点我也不希望再空格键为preSS'他们。

我想相同的功能在Windows计算器的按钮。

Google搜索和搜索堆栈一切似乎是有关表单如。 制作形式在C#不可作为焦点

我知道我应该重写的WndProc,但不完全知道如何着手,以我应该抓住什么样的信息/忽略等。据我得到的答案:

 保护覆盖无效的WndProc(参考消息M)
    {
        base.WndProc(REF米);
    }


解决方案

所有你要做的就是这一行添加到关键的点击活动结束:

  this.Focus();

这条线将导致按钮失去焦点,窗体将获得焦点和空格不会有任何影响,从而满足您的2个条件。

现在,如果你不想让按钮能够再次点击,然后添加这两条线来代替:

  this.Focus();
((按钮)发送方).Enabled = FALSE;

这将做什么其他行做,此外,它会禁用按钮。

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.

I want the same functionality as the buttons in windows calculator.

Googled and searched stack everything seems to be about forms eg. Make a form not focusable in C#

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);
    }

解决方案

All you got to do is add this line to the end of the key's Click event:

this.Focus();

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆