AcceptButton not working


  • I am setting the default button on my form, like so:

    this.AcceptButton = this.noButton;

    But the other button ("Yes") button click is still being envoked. In the designer the noButton appears to be set as the default (it has an outline). Then when I run, the"Yes" button has an outline and acts as the default. The debugger also seems to think that the noButton is the default button:



    [System.Windows.Forms.Button]: {Text="No"}

    DialogResult: None

    Any thoughts/ideas?

    Thursday, July 27, 2006 11:34 PM

All replies

  • Click on another control on your form - a textbox or radio button or something.  I suspect you'll find that your No button gets outlined as you expect.

    The AcceptButton identifies the button that should be 'clicked' if someone types enter in some other control (like a textbox).  However, the control that has focus when the form is first displayed is the control with a tabindex value of zero.  So, if your "yes" button has tab index zero, that's the button that will have focus when the form is first displayed.

    If you press enter while a button has focus that's treated as you clicking on the focused button - which takes precedence over the AcceptButton behaviour .

    Either set your no button's tabindex to zero or set some other non-button control's tabindex to zero and I'm guessing you'll get the behaviour you want.

    Friday, July 28, 2006 1:19 AM
  • I am seeing the same problem.


    I set my form's AcceptButton, I call the button's NotifyDefault, and I call Focus() on the button...

    and it displays with the wrong button highlighted and the wrong button is fired upon <Enter>.  I've rearranged TAB order to no avail.


    Oddly, I've discovered if I make subsequent calls to the form's ShowDialog method, then the screen looks right, but the wrong button still gets fired.


    And, as in your case, the debugger says the AcceptButton is the one I want.


    I can click all over the form and nothing changes... UNLESS I click on the desired button... in which case subsequent calls to ShowDialog work as expected... the desired button is highlighted and that button fires when <Enter> is pressed.


    I'm using Visual Studio 2005 / .NET 2.0    Is there a known problem or is there some other voodoo required?



    Sunday, December 02, 2007 4:31 AM
  • DUH - If I programatically change the tab index with the assignment of AcceptButton I get the result I need.


    The answer was in the reply... I was just a little slow in digesting it.  Thanks.  My form is essentially a MessageBox - it contains only an icon, a label and a couple buttons.  The buttons are the only active controls on the form, so one of them always had tab order 0, so the other would never be the working AcceptButton.  I don't understand why the call to desiredButton.Focus() didn't take care of it... but I won't worry about it.  I've already spent far too long on this!

    Sunday, December 02, 2007 7:07 PM
  • private void textbox1_Enter(object sender, EventArgs e)
                this.AcceptButton = null;
            private void textbox1_Leave(object sender, EventArgs e)
                this.AcceptButton = this.button1;

    Thursday, December 25, 2014 6:00 AM