0

This seems very basic but have not been able to find any information on it.

I have a button in a Windows form that is the AcceptButton and should always have the AcceptButton behavior i.e. pressing the enter key when focus is on the form should always click the button and it should always be highlighted to show that action is available.

However, when a different button is clicked, that button is now highlighted and when enter is pressed, that button is clicked instead of the AcceptButton.

Here is my test application that shows the behavior:

    public Form1()
    {
        InitializeComponent();

        AcceptButton = btnAccept;
    }

    private void btnAccept_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You clicked the accept button");
        Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
    }

    private void btnNothing_Click(object sender, EventArgs e)
    {
        Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();
        Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
    }

When the application launches initially - it works as I expect, btnAccept is highlighted and enter clicks that button.

However, when I click btnNothing or btnBrowse, that button is now highlighted and pressing enter clicks that button again. Additionally, the debug lines always show that btnAccept is still the AcceptButton even when the enter button is clicking a different button.

enter image description here

The CanFocus property that Button inherits from Control looks promising, but this property is read only.

What's going on here? Is there some property I can use to change this behavior?

Edit

Ended up going with this solution based on @AWinkle 's answer:

    public Form1()
    {
        InitializeComponent();

        AcceptButton = btnAccept;
    }

    private void btnAccept_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You clicked the accept button");
    }

    private void btnNothing_Click(object sender, EventArgs e)
    {
        Button acceptButton = (Button)this.AcceptButton;
        acceptButton.Select();
    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog1.ShowDialog();

        Button acceptButton = (Button)this.AcceptButton;
        acceptButton.Select();
    }
hwustrack
  • 67
  • 3
  • 8
  • 2
    This is the normal Windows behavior. If a button has the focus then the Enter key is handled by that button and not passed around. – Steve May 24 '17 at 18:53
  • Your focus is on `"Nothing" Button` and not on the `Form`. You should take focus to `Form` after `"Nothing"` and `"Browse"` buttons clicks if you want to click with enter `"Accept"` button – Samvel Petrosov May 24 '17 at 18:56
  • 1
    You could add this to the click event handlers of Nothing and Browse: `this.Focus();` and when someone clicks on the button, it will process the other handling code and then reset the focus to the form. As a user of the application, this might get confusing. – AWinkle May 24 '17 at 19:01
  • 1
    If you do want to bypass the standard Windows behavior, you could make your own button that inherits from the WinForms button and cancel the event if `this != ((Form)this.Parent).AcceptButton)`. [Some additional reading](https://stackoverflow.com/questions/1298640/c-sharp-trying-to-capture-the-keydown-event-on-a-form) – AWinkle May 24 '17 at 19:08
  • I tried @AWinkle 's first answer first. Calling `this.Focus();` in the event handlers of Nothing and Browse did not seem to work, that method returns false. Is there something I'd have to do first to make the form focusable? I did use that idea though for a slightly different workaround: `Button acceptButton = (Button)this.AcceptButton;` `acceptButton.Focus();` – hwustrack May 24 '17 at 20:56
  • For @AWinkle 's second answer, which event would I want to override to cancel? GotFocus? – hwustrack May 24 '17 at 21:01
  • @hwustrack `KeyDown` – AWinkle May 25 '17 at 14:04

0 Answers0