6

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);
    }
Community
  • 1
  • 1
StudentJ
  • 63
  • 1
  • 1
  • 3

3 Answers3

25

I dealt with this problem today, and below is the answer that was easiest for me. I didn't want to use this.Focus() because I needed focus to remain unchanged.

http://social.msdn.microsoft.com/Forums/windows/en-US/f1babeac-4bd9-498f-b19b-90b9fed0d751/c-stop-button-from-gaining-focus-on-click

Create your own button class that can't be selected.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YourNameSpaceHere {
    class NoSelectButton : Button{

        public NoSelectButton() {

            SetStyle(ControlStyles.Selectable, false);

        }
    }
}

Now, go update the design file with NoSelectButton instead of the System's version. Should be in two locations per instance.

Nb: The Visual Studio designer may momentarily break its preview until you press Start.

C4F
  • 662
  • 7
  • 18
  • 2
    This has the side effect that `PerformClick()` stops firing events. But this can be worked around: http://stackoverflow.com/questions/16951142/performclick-on-custom-button-will-not-work/31467870#31467870 – Loathing Jul 17 '15 at 03:16
7

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.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • Key's click event ? What I want is to be able to click the button but for it not to gain focus no matter how many times I click it. Like the windows calculator buttons. If you meant adding it to the buttons click event that will not work. – StudentJ Jul 19 '11 at 02:21
  • @StudentJ - I know what you are trying to do. If you want to emulate the Windows Calculator function, then add `this.Focus();` to the button's `Click` event. Trust me, it'll do what you want. Because what will happen is when it executes the `this.Focus();` line, focus will go off the button and back to the form. – Icemanind Jul 19 '11 at 02:26
  • 1
    It won't work the focus is still maintained on the button and will not change to the form. Even tried it with a brand new form and just 1 button. – StudentJ Jul 19 '11 at 03:07
  • StudentJ -- Try adding the code to the Button's `MouseButtonUp` event – Icemanind Jul 19 '11 at 03:44
  • Tried click,mouseclick and mousebuttonup still the same result. I understand the code and why it should work but it doesn't. – StudentJ Jul 19 '11 at 13:04
  • 5
    Ok got it to work, had to pass focus on button click to another control that could get focus. Apparently labels and forms do not get a focus like textbox and other input elements. – StudentJ Jul 19 '11 at 14:03
  • What about making sure the TabStop property on each control is set to False? – Jan Tacci Aug 27 '12 at 21:46
1

As mentioned in the comments, I find the easiest way to do this is to add an input control to the form that does nothing (has no event handlers), and then in the OnClick method of the button use Control.Focus(). However, please note that this won't work if you set the dummy control's Visible property to false.

So my solution was to add a dummy button, set its size to 0, 0 and set the focus to that each time. I suppose you could also place the control behind something else or outside the bounds of a non-resizable form or panel.

kad81
  • 10,712
  • 3
  • 38
  • 44