如何停止使用像&QUOT键盘按键按下按钮,空格键或Enter]键;. C# [英] How to stop pressing button using keyboard keys like "Spacebar or Enter". C#

查看:463
本文介绍了如何停止使用像&QUOT键盘按键按下按钮,空格键或Enter]键;. C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个按钮,赢得泡沫的应用程序。当我运行该应用程序,我能够按使用鼠标点击还可以通过按键盘上的键这些按钮这些按钮。我不想按使用键盘键这些按钮。

I have win-foam application having several buttons. When i run this application, i am able to press these buttons using mouse click also able to press these buttons using keyboard keys. I don't want to press these buttons using keyboard keys.

我也想停下来专注于按钮,当我点击选项卡或箭头键^,V, <,>

I also want to stop focus on buttons when i click "Tab" or arrows keys"^,v,<,>".

问候

推荐答案

相反的标准按钮,请使用以下,当你需要这种行为

Instead of the standard Button, use the following when you need that behavior

public class NonSelectableButton : Button
{
    public NonSelectableButton()
    {
        SetStyle(ControlStyles.Selectable, false);
    }
}



编辑:
这里是一个小的测试证明了它的工作使用系统

Here is a little test proving that it's working

using System;
using System.Linq;
using System.Windows.Forms;

namespace Samples
{
    public class NonSelectableButton : Button
    {
        public NonSelectableButton()
        {
            SetStyle(ControlStyles.Selectable, false);
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            Control[] controls = { new TextBox(), new TextBox(), };
            Button[] buttons = { new NonSelectableButton { Text = "Prev" }, new NonSelectableButton { Text = "Next" }, };
            foreach (var button in buttons)
                button.Click += (sender, e) => MessageBox.Show("Button " + ((Button)sender).Text + " clicked!");
            int y = 0;
            foreach (var item in controls.Concat(buttons))
            {
                item.Left = 8;
                item.Top = y += 8;
                form.Controls.Add(item);
                y = item.Bottom;
            }
            Application.Run(form);
        }
    }
}



EDIT2::要应用的解决方案,你需要做到以下几点:

: To apply the solution, you need to do the following:

(1)一个新的代码文件添加到您的项目,把它与NonSelectableButton.cs以下内容

(1) Add a new code file to your project, call it NonSelectableButton.cs with the following content

using System;
using System.Linq;
using System.Windows.Forms;

namespace YourNamespace
{
    public class NonSelectableButton : Button
    {
        public NonSelectableButton()
        {
            SetStyle(ControlStyles.Selectable, false);
        }
    }
}



(2)编译项目结果
(3)现在,新的按钮将出现在控件工具箱(在顶部),你可以拖动窗体上的而不是一个标准的按钮。

这篇关于如何停止使用像&QUOT键盘按键按下按钮,空格键或Enter]键;. C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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