How to stop pressing button using keyboard keys like “Spacebar or Enter”. C#

前端 未结 3 1888
长发绾君心
长发绾君心 2020-12-22 01:09

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 ke

相关标签:
3条回答
  • 2020-12-22 01:16

    Looks like you have troubles applying my previous answer. Here is another way using the same idea:

    Add a new code file to your project and put the following code inside (make sure to replace YourNamespace with yours!)

    using System;
    using System.Reflection;
    using System.Windows.Forms;
    
    namespace YourNamespace
    {
        public static class Utils
        {
            private static readonly Action<Control, ControlStyles, bool> SetStyle =
                (Action<Control, ControlStyles, bool>)Delegate.CreateDelegate(typeof(Action<Control, ControlStyles, bool>),
                typeof(Control).GetMethod("SetStyle", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(ControlStyles), typeof(bool) }, null));
            public static void DisableSelect(this Control target)
            {
                SetStyle(target, ControlStyles.Selectable, false);
            }
        }
    } 
    

    Then use it inside your Form Load event for each button you need to have that behavior.

    For instance, if your form contains 2 buttons called btnPrev and btnNext, include the following lines in your form load event

    btnPrev.DisableSelect();
    btnNext.DisableSelect();
    
    0 讨论(0)
  • 2020-12-22 01:18

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

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

    EDIT: 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) 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) Compile the project
    (3) Now the new button will appear in the control toolbox (at the top) and you can drag it on a form instead of a standard button.

    0 讨论(0)
  • 2020-12-22 01:24

    The best I could work out for stopping all buttons responding to the space-bar is this:

    NB: It is a bit of a hack:

    Set your form's KeyPreview property to true.

    Then add this code to the form's KeyPress event:

    private void Form1_KeyPress(object sender, KeyPressEventArgs e) 
    {
       if (this.ActiveControl is Button
       && e.KeyChar == (char)Keys.Space) 
       {
          var button = this.ActiveControl;
          button.Enabled = false;
          Application.DoEvents();
          button.Enabled = true;
          button.Focus();
       }
    }
    

    And to stop a control from getting focus when tabbing, simply set the button's TabStop property to false.

    0 讨论(0)
提交回复
热议问题