1

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.

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

Regards

Muhammed Naqi
  • 47
  • 1
  • 9
  • You should disable the button so you can't tab to them. – Dbuggy Sep 28 '15 at 12:59
  • 2
    Plug off your keyboard – Bgl86 Sep 28 '15 at 13:00
  • When I change Enable property into False then button also cannot press using mouse click. – Muhammed Naqi Sep 28 '15 at 13:10
  • possible duplicate of [How to disable navigation on WinForm with arrows in C#?](http://stackoverflow.com/questions/1318236/how-to-disable-navigation-on-winform-with-arrows-in-c), the same can concept be applied to any button really. – Oceans Sep 28 '15 at 13:13
  • i Used this code but space bar is still working. – Muhammed Naqi Sep 28 '15 at 13:28
  • i Used this code but space bar is still working. "private void Form1_Load(object sender, EventArgs e) { foreach (Control control in this.Controls) { control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown); } } void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space) { e.IsInputKey = true; } }" – Muhammed Naqi Sep 28 '15 at 13:41

3 Answers3

5

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.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
4

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.

Ulric
  • 826
  • 5
  • 16
1

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();
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343